Skip to content

Unsafe declaration of resolve function in Promise constructor #22040

Closed
@ikokostya

Description

@ikokostya

TypeScript Version: typescript@2.8.0-dev.20180217

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

Activity

added this to the TypeScript 2.9 milestone on Mar 10, 2018
rbuckton

rbuckton commented on May 9, 2018

@rbuckton
Contributor

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

added
Design LimitationConstraints of the existing architecture prevent this from being fixed
and removed
BugA bug in TypeScript
on May 10, 2018
removed this from the TypeScript 3.0 milestone on May 10, 2018
ikokostya

ikokostya commented on May 15, 2018

@ikokostya
ContributorAuthor

@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

RyanCavanaugh commented on May 15, 2018

@RyanCavanaugh
Member

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

typescript-bot

typescript-bot commented on May 30, 2018

@typescript-bot
Collaborator

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

locked and limited conversation to collaborators on Jul 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ikokostya@rbuckton@RyanCavanaugh@mhegazy@typescript-bot

        Issue actions

          Unsafe declaration of resolve function in Promise constructor · Issue #22040 · microsoft/TypeScript