Skip to content

Promise being resolved with wrong type #9991

Closed
@0xfede

Description

@0xfede

TypeScript Version: 1.8.10 and 2.0.0 beta

Code

function getPromise(): Promise<string> {
  return new Promise(resolve => {
    resolve(1234); // Should fail as 'abcd' is not a number
  });
}

getPromise().then(s => {
  console.log(s.length);
});

Expected behaviour: A compile error on line 3

Actual behaviour: The program prints undefined

If I copy the type definition of resolve from the interface PromiseConstructor (in lib.es6.d.ts), I get the expected behaviour:

function getPromise(): Promise<string> {
  return new Promise((resolve: (value?: string | PromiseLike<string>) => void) => {
    resolve(1234); // As expected I get an error
    // TS2345: Argument of type 'number' is not assignable to parameter of type 'string | PromiseLike<string>' Type 'number' is not assignable to type 'PromiseLike<string>'.
  });
}

getPromise().then(s => {
  console.log(s.length);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions