Michal Ostruszka Michal Ostruszka - 1 year ago 81
TypeScript Question

Isn't promise type parameter checked in typescript?

Why doesn't it generate typescript compiler warning but just fails at runtime when I try to work on foo as


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
I get one, but that should not be the required as TS compiler is smart enough to infer type of the RHS as
when inspected. So why it doesn't yell at me when I try to do this assignment?
And is there any workaround for that other than specifying type explicitly? I'm using TS 2.0.9.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download