microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.75k stars 12.46k forks source link

The Promise constructor cannot handle void returns on resolve #45162

Open Sc0tTyXL opened 3 years ago

Sc0tTyXL commented 3 years ago

Configuration Check

My compilation target is es5 and my lib is dom, es5, es2015.promise and es2015.iterable.

Missing / Incorrect Definition

The value for the resolve callback in the Promise constructor is required: new <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;

Sample Code

Using void as the generic (return) type for a Promise results in an error: image

Workaround

The current workaround is to return undefined: image

But undefined is not the same as void: https://www.typescriptlang.org/docs/handbook/2/functions.html#void

MartinJohns commented 3 years ago

What TypeScript version are you using? It works fine with 4.3.5:
https://codesandbox.io/s/typescript-playground-export-forked-ej4hy?file=/index.ts

Sc0tTyXL commented 3 years ago

We were running v4.3.2 and updated to v4.3.5 but no joy.

MAhmadIqbal commented 1 month ago

hi @Sc0tTyXL

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "es5", "es2015.promise", "es2015.iterable"]
  }
}

even you are using es2015.promise and es2015.iterable but its runtime compiler wont use promises without polyfil. use polyfil to execute promise base execution at run time in es2015

npm i core-js
//AND
import 'core-js/es6/promise'

use promise like we do in es6 and passing nothing in resolve would be interpreted as **void**
const myPromise: Promise<void> = new Promise<void>((resolve, reject) => {
  setTimeout(() => {
    console.log("Async operation completed");
    resolve();  // No value returned --void--
  }, 1000);
});