Open
Description
The type checking of promises has several issues with it.
Promise.resolve
is not type checked correctly when called without argument
/**
* @type {Promise<string>}
*/
const p = Promise.resolve() // should raise error, it resolves to undefined, not string
- Chained promises are not type checked correctly
/**
* @type {Promise<string>}
*/
const p = Promise.resolve().then(function() { return 555 }) // should raise error, it resolves to number, not string
new Promise
is not type checked correctly against the type of the variable it is being assigned to
/**
* @type {Promise<string>}
*/
const p = new Promise((resolve, reject) => {
resolve(666) // should raise error, it resolves to number, not string
})
new Promise
is not type checked correctly against the type of the variable it is being assigned to even if callbacks are typed explicitly
/**
* @type {Promise<number>} // should raise error, it resolves to string, not number
*/
const p = new Promise(
/**
* @param {function(string): void} resolve
* @param {function(string): void} reject
*/
(resolve, reject) => {
resolve('Hello')
}
)
Extra question: It is possible that I am just not using the typing syntax correctly. All I want is to assign a new promise to a variable and have strict type checking of both the variable and the promise callbacks. Is there a way to do that?
const p = new Promise((resolve, reject) => { resolve(555) })
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;