Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow: How do I type-annotate a local/global variable?

If I have the process.browser variable in my code how can I annotate it?

like image 915
Yanis Urbis Avatar asked Jun 05 '17 08:06

Yanis Urbis


1 Answers

You can declare the process variable:

// @flow

declare var process: { browser: number }

const foo = process.browser * 3

Here is a sample of it in action: https://flow.org/try/#0PTAEAEDMBsHsHcBQiAmBTAxtAhgJzaAG56gAOusGaAztQFygDeoARhfNWrgwHYCuAWxZdQAX2QZYPagBdQkWLFABeMhSq0AdGwSdcoAFSgAzIiA

In this example the global variable is declared locally in the file that consumes it, however it might be more ideal to declare this globally via a library definition. Going this route, it will automatically be defined for every file.

For example, add the following to <PROJECT_ROOT>/flow-typed/process.js and you should be set. You may need to restart the Flow server.

declare var process: { browser: number }
like image 77
Michael LaCroix Avatar answered Oct 12 '22 03:10

Michael LaCroix