Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flowtype - Can flowtype automatically annotate the variables' type?

I have searched the doc of flowtype but I can not find something relevant to type inference, for example:

function add(x){
  return x+10;
}

after using flowtype, it becomes:

function add(x:string){
   return x+10;
}

just like jsnice.

like image 957
Xuxinyi Avatar asked Apr 09 '26 00:04

Xuxinyi


2 Answers

There is cli command flow suggest <yourfile>. Unfortunately it is not as powerful as jsnice and will not help you with this case, but if you apply it for example to

function sub(x, y){
    return x - y;
}

it will suggest you

function sub(x, y): number{
    return x - y;
}

so it is more or less useful.

You can also try to use first the jsnice and then flow-jsdoc which converts jsdoc annotations to flow.

like image 70
timofeyka Avatar answered Apr 11 '26 14:04

timofeyka


flow suggest is indeed the way to go. In general it cannot infer input types for functions that are exported (only un-exported functions used locally in your file), but it can fill in output types of all functions, the types of local variables, etc.

Also note that Flow doesn't infer polymorphic types.

like image 21
Avik Chaudhuri Avatar answered Apr 11 '26 13:04

Avik Chaudhuri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!