Re:TypeScript 2.3
by ng12 on Thursday April 27, 2017 @02:36PM
(#14213731)
FWIW your Promise example has to do with whatever implementation of Promise you're using, not Typescript. Bluebird's typings would correctly complain with that example -- both because your second Promise is implicit-any, and the resolve is wrong.
[ Parent
| on HN ]
Re:TypeScript 2.3
by andreypopp on Thursday April 27, 2017 @02:46PM
(#14213787)
EDIT 2: This still surprises me...
let promisedString: Promise<string> = new Promise<string>(
resolve => {
// Perfectly valid in TypeScript!
resolve(undefined)
}
)
EDIT: no, I was wrong — Bluebird typings won't allow this behaviour but they force me to annotate both the binding and the Promise constructor call: let promisedString: Promise<string> = new Promise<string>(
resolve => {
resolve(42) // type error here, ok
}
)
That's surprising TS ships with such unsafe typings for ES2015 Promise API...Hm... no.
First, note that this is with noImplicitAny activated. You can check this in TS playground.
Then if we inspect what typings TS have for ES2015 Promises[1] and for Bluebird[2], you can clearly see that those are identical in this case..
[1]: https://github.com/Microsoft/TypeScript/blob/master/lib/lib....
[2]: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/mast...
Er, I'm looking at it right now? I don't know what to tell you.
error TS2322: Type 'Bluebird<{}>' is not assignable to type 'Bluebird<string>'.
And if I add the type parameter to the constructor... error TS2345: Argument of type '42' is not assignable to parameter of type 'string | Thenable<string>'
Re: your edit, you actually don't need that first typing. Typescript will infer the type of the promisedString.
Re:TypeScript 2.3
by andreypopp on Thursday April 27, 2017 @02:56PM
(#14213850)
> your edit, you actually don't need that first typing. Typescript will infer the type of the promisedString
Ok, but still this is confusing, it can't infer the type of RHS given the annotated LHS? Intuitively there's nothing wrong with it and Flow does that well...
Re:TypeScript 2.3
by mcintyre1994 on Thursday April 27, 2017 @04:31PM
(#14214443)
Note this is similar to c# (and thinking about it, every OO language I can think of):
var cat = new Animal<Cat>(...); // yup! Animal<Cat> cat = new Animal(...); // Nope
I wonder if internally ts is treating these generic-accepting classes in the same way, where the generic is required?