closure-compiler icon indicating copy to clipboard operation
closure-compiler copied to clipboard

Interfaces do not check return type of methods when `return` is not called

Open fingerartur opened this issue 2 years ago • 2 comments

Interfaces do not check return type of methods when return is not called.

/** @interface */
function ISerializable() {};
/** @type {function(): string} */
ISerializable.prototype.serialize;

/**
* @implements {ISerializable}
* @constructor
*/
function Car() {}
Car.prototype.serialize = function() {}
// Does not produce error, and it should. The interface says this method
// must return a string, yet it returns void and there is no error

The interface says method serialize must return a string, yet it returns void and there is no error. There should be an error.

Compiler Version: v20221102

Build command:

java -jar ./scripts/closureCompiler.jar \
  --entry_point=./src/js/index.js \
  --js=./src/**.js \
  --dependency_mode=PRUNE \
  --warning_level=VERBOSE \
  --js_output_file=./dist/bundle.js \
  --module_resolution=WEBPACK \
  --compilation_level=ADVANCED \
  --jscomp_error=checkDebuggerStatement \
  --jscomp_error=unusedLocalVariables \
  --jscomp_error=reportUnknownTypes \
  --jscomp_error=strictCheckTypes;

fingerartur avatar Jan 09 '23 20:01 fingerartur

Note: This only happens for empty functions since their return type is looser. This should report as expected for non-empty functions even with no return.

rishipal avatar Jan 11 '23 17:01 rishipal

Confirmed

fingerartur avatar Jan 13 '23 09:01 fingerartur