Yanis Urbis
Yanis Urbis

Reputation: 399

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?

Upvotes: 10

Views: 5205

Answers (1)

Michael LaCroix
Michael LaCroix

Reputation: 5808

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 }

Upvotes: 12

Related Questions