Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare environment variable types with flow js?

Environment variables accessed from process.env are considered undefined when running flow since the parser has no idea what is included inside of them.

How do I tell the parser that the env object exists and what keys does it include?

like image 309
AJ_1310 Avatar asked Dec 01 '25 14:12

AJ_1310


1 Answers

The contents of environment variables is like any other input given at runtime: it cannot be known statically. Therefore Flow must be conservative and force you to validate it with runtime checks. Here are two examples of runtime checks that work (copied from https://github.com/facebook/flow/issues/1192#issuecomment-299140919)

// throw
if (!process.env.FOO) throw new Error('FOO missing');
const foo = process.env.FOO;

// fall back
const bar = process.env.BAR || 'bar';

(foo: string); // ok!
(bar: string); // ok!
like image 177
Nat Mote Avatar answered Dec 04 '25 05:12

Nat Mote



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!