Skip to content
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

Promise being resolved with wrong type #9991

Closed
0xfede opened this issue Jul 27, 2016 · 5 comments
Closed

Promise being resolved with wrong type #9991

0xfede opened this issue Jul 27, 2016 · 5 comments
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@0xfede
Copy link

0xfede commented Jul 27, 2016

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);
});
@jeffreymorlan
Copy link
Contributor

It's easier to specify the type argument: new Promise<string>(resolve => {...})

Unfortunately, new Promise(resolve => {...}) without any type annotations always has a type of Promise<{}> due to #5254, which is assignable to any other Promise due to #6407.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@badescuga
Copy link

badescuga commented Dec 6, 2017

I'm also seeing this :) Not sure why the type is not being respected. TS 2.6.2

type Dictionary<T> = { [id: string]: T };

 public getGamesById(gameIds: string[]): Promise<Dictionary<Game>> {
const games: Game[];
return Promise.resolve(games);
}

@waratah
Copy link

waratah commented Jan 28, 2018

@badescuga any is a catch all and will totally break type safety. Got me last week. The more definitions you introduce in your code the better. You can also use noImplicitAny to pick up on these errors.

@RyanCavanaugh
Copy link
Member

This has been fixed

@acashjos
Copy link

acashjos commented Oct 5, 2019

TypeScript Version: Version 3.6.3

Just a reminder, while the fix works for most practical cases, it still doesn't detect type inconsistencies for Promise<Object > , Promise<{}> or any interface defined as {}

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

getPromise().then(s => {
   console.log(s.length);  // fails as length is not a property of Message
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

6 participants