Flow中的非null和非undefined类型

时间:2017-01-01 21:39:34

标签: flowtype non-nullable

Flow定义了所谓的"Maybe types"。即?stringstring | null | void类似(void是一种值undefined)。

是否存在类似于nullundefined的任何值的常规类型?如果$Diff<$Diff<any, null>, void>运算符,基本上类似于$Diff能够在非对象类型上操作。

2 个答案:

答案 0 :(得分:5)

没有一些&#34;魔法&#34;为此类型,但这样的事情应该有效:string | number | boolean | {} | []

答案 1 :(得分:0)

可以使用NonMaybeType Flow实用程序类型:请参见$NonMaybeType

  

$NonMaybeType<T>将类型T转换为非也许类型。换句话说,$NonMaybeType<T>的值是T的值,除了null和undefined之外。

// @flow
type MaybeName = ?string;
type Name = $NonMaybeType<MaybeName>;

('Gabriel': MaybeName); // Ok
(null: MaybeName); // Ok
('Gabriel': Name); // Ok
(null: Name); // Error! null can't be annotated as Name because Name is not a maybe type