For your second example - it's a divisive issue. I believe that a Hindley-Milner style type resolver (what Flow uses) is more usable, but there is a strong argument against it too [0].
That's something I don't understand. Isn't that incorrect behaviour? Shouldn't TS try unify RHS and LHS?
In this case in Typescript it is generally more strict to add a type annotation to the generic than the LHS:
let promise = new Promise<string>((resolve) => resolve(42)) // This won't work
This is as much because the inference here that matters isn't between LHS and RHS but between generic arguments and function return inference.For example:
function fn(a) {
const {b} = a;
console.log(b.c);
}
fn({});
This compiles without errors - but would fail at runtime. https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVUCuA7AxgFwEs5swp... 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...
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.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...
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?
let promisedString = new Promise<string>(
resolve => {
// Fails as expected in TypeScript!
resolve(42)
}
)And in personal anecdote-space I've experience the exact opposite :)
What I like about TypeScript and which I think is "sound" compared to Flow is the whole experience and how it is backed by good tooling.
With TypeScript I can open almost "any" editor and expect it to understand and respect the typings of all my TypeScript code. It will correctly autocomplete, and tell me when I'm using undefined members.
Basically with TypeScript every part of the stack seems to work for me, the developer, to ensure I make the right decisions and get the assistance I need, at all time. That makes it feel like a very safe and sound language to write code in.
With flow? Well fuck that. From what I can tell, you get almost nothing.
You can run your compiler and see if it validates, but I've seen almost no editor-support worth mentioning, meaning that while the type-inference in the compiler may occasionally be better (but for how long? TS has a ton of momentum), the overall experience feels much less sound and much more fragile. Much more unsafe.
The type-declarations definitely feel as important as the way they are declared: they're mere comments. They're not real.
The main takeaway for me is that TS is much much less sound than Flow. That means it checks less invariants which in turn makes you less trust it...
Consider the following code which is valid (!) in TS:
Another downside of TS is poor type inference. It requires to annotate each top level function while Flow only requires to annotate exported functions.Even the following simple piece of code will make TS ask you to annotate doAddition function:
I'm really glad Flow doesn't force me into it.Edit: spelling