Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are promise type parameters checked in TypeScript?

Why doesn't the following example generate a TypeScript compiler warning (instead failing at runtime) when I try to work on foo as Promise<string>?

let foo: Promise<string> = Promise.resolve(10); // should not compile
foo.then(v => v.toUpperCase());

// -> TypeError: v.toUpperCase is not a function

Only if I put Promise.resolve<string>(10) do I get an error, but that should not be the required as the TypeScript compiler is smart enough to infer type of the RHS as Promise<number> when inspected. So why it doesn't yell at me when I try to do this assignment? Also, is there any workaround for that other than specifying type explicitly? I'm using TypeScript 2.0.9.

like image 921
Michal Ostruszka Avatar asked Dec 05 '25 11:12

Michal Ostruszka


1 Answers

This appears to be a known issue (also see #10524, which is the main tracking issue for this problem) and should be fixed in v2.1.2 by the linked commit. You should be able to upgrade and the types will then be enforced properly, as intended.

If the issue persists in v2.1.x, my suggestion would be to report a bug in their issue tracker.

like image 93
Aurora0001 Avatar answered Dec 06 '25 23:12

Aurora0001