First of all, I am mostly familiar with TypeScript. Flow looks very similar to TS in many ways, but I recently stumbled upon an asterisk (*) type. At first, I thought it was a synonym for "any", but now, after reading some of the release notes for Flow, I see that it's not. I skimmed through all the official docs and was not able to find any usage of "*".
So, what is it and when to use it? But also, what would be a direct equivalent of that in TypeScript?
Edit: Since I originally wrote this answer, I have learned that *
is unsafe when it appears at module boundaries. I can't recommend using it, and it may be removed in the future.
It just tells Flow to infer a type parameter, rather than making you write it out explicitly:
function foo(): Array<*> {
return [5];
}
// Flow issues an error:
// 2: return [5];
// ^ number. This type is incompatible with
// 10: (foo(): Array<string>);
// ^ string
(foo(): Array<string>);
(try flow)
It is different from any
-- any
is an unsafe type, so if you replaced *
with any
in this example, Flow would not give you any errors. You could replace it with number
and Flow would give you a similar error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With