Skip to content

Type checking of Promise does not work very well #4040

Open
@fingerartur

Description

@fingerartur

The type checking of promises has several issues with it.

  1. 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
  1. 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
  1. 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
})
  1. 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) })

Related: #2894, #2402

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions