2017-05-03 71 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!