Skip to content

Unsafe declaration of resolve function in Promise constructor #22040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ikokostya opened this issue Feb 19, 2018 · 4 comments
Closed

Unsafe declaration of resolve function in Promise constructor #22040

ikokostya opened this issue Feb 19, 2018 · 4 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@ikokostya
Copy link
Contributor

ikokostya commented Feb 19, 2018

TypeScript Version: [email protected]

Search Terms:

Promise, resolve

Code

function f(): Promise<number> {
    return new Promise((resolve) => {
        resolve();
    });
}

tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "moduleResolution": "node",
        "target": "es2017",
        "lib": ["es2017"],
        "alwaysStrict": true,
        "noEmitOnError": true,
        "pretty": true,
        "strict": true
    }
}

Expected behavior:

Compile error: undefined is not assignable to number.

Actual behavior:

No errors.

Playground Link: http://www.typescriptlang.org/play/index.html#src=function%20f()%3A%20Promise%3Cnumber%3E%20%7B%0A%20%20%20%20return%20new%20Promise((resolve)%20%3D%3E%20%7B%0A%20%20%20%20%20%20%20%20resolve()%3B%0A%20%20%20%20%7D)%3B%0A%7D

Looks like this happens, because resolve() function in Promise executor takes optional argument https://github.com/Microsoft/TypeScript/blob/b3edc8f9f4d9cf4203c4c4493e4f0f3dc96c845d/lib/lib.es2015.promise.d.ts#L33

The provided example is correctly checked by Flow: https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVUCuA7AxgFwEs5swoAKASgC4wAFAJzgFtCBnAUwB5tNmAjDgwB8YAN6owUsAw75MDUtg5JGLdh3LlZbODABuHSmAC8oidMsyOug5soBuSdIC+j1C9RA


I trying to change declaration (i.e. make value parameter of resolve function is required) and the following code stop working:

function bar(): Promise<void> {
    return new Promise((resolve) => {
        resolve(); // Expected 1 arguments, but got 0.
    });
}

In Flow this code also works without error https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVUCuA7AxgFwEs5swAjAQwCcAKASgC4wAFKuAW0IGcBTAHgBucQgBMAfGADeqMLLBUe+TFVLYeSVh248aNBVzgwBPOmAC8E6XOvyeBozroBuGXIC+z1G6A

@mhegazy mhegazy added the Bug A bug in TypeScript label Mar 10, 2018
@mhegazy mhegazy added this to the TypeScript 2.9 milestone Mar 10, 2018
@rbuckton
Copy link
Contributor

rbuckton commented May 9, 2018

We have already tried to address this, but unfortunately this resulted in either Promise no longer being extensible or other breaks.

@mhegazy mhegazy added Design Limitation Constraints of the existing architecture prevent this from being fixed and removed Bug A bug in TypeScript labels May 10, 2018
@mhegazy mhegazy removed this from the TypeScript 3.0 milestone May 10, 2018
@ikokostya
Copy link
Contributor Author

@mhegazy @rbuckton Can you provide more detailed description why this is Design Limitation? From my point of view need ad-hoc solution to allow call resolve() without undefined argument for Promise<void> type.

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented May 15, 2018

@ikokostya #22772 (comment) goes into a fair bit of detail on this

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

5 participants