2017-05-03 40 views
2

process.env访问的环境变量在运行flow时被视为未定义,因为解析器不知道其中包含的内容。如何用流js声明环境变量类型?

如何告诉解析器env对象存在以及它包含哪些关键字?

+1

https://github.com/facebook/flow/issues/1192 –

回答

1

环境变量的内容与在运行时给出的任何其他输入类似:它不能被静态地识别。因此Flow必须保守,并强制您使用运行时检查进行验证。下面是该工作(从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!