closure-compiler
closure-compiler copied to clipboard
`Array.pop` and `Array.shift` methods do not get type-checked correctly
Array.pop and Array.shift methods do not get type-checked correctly, at least not by Typescript standards.
Example:
/**
* @param {Array<string>} names
*/
function hello(names) {
/** @type {string} */
const name = names.pop() // should raise an error, `name` can be undefined
}
/**
* @param {Array<string>} names
*/
function hello(names) {
/** @type {string} */
const name = names.shift() // should raise an error, `name` can be undefined
}
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;
The reason for this behavior is that the standard externs definitions for these methods don't include undefined as a possible return value.
e.g.
https://github.com/google/closure-compiler/blob/a4329316423d8478452e6330c6b887dd97ddef00/externs/es3.js#L860-L869
The trouble with fixing this is that it will likely cause a large amount of existing code to start failing to compile with type errors.
However, for a new project, you could definitely use modified versions of the externs that have more accurate definitions.