Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of asterisk (*) type in Flow and what is the equivalent of that in TypeScript?

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?

like image 746
NeverwinterMoon Avatar asked Sep 07 '25 05:09

NeverwinterMoon


1 Answers

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.

like image 68
Nat Mote Avatar answered Sep 10 '25 02:09

Nat Mote