Flow定义了所谓的"Maybe types"。即?string
与string | null | void
类似(void
是一种值undefined
)。
是否存在类似于null
和undefined
的任何值的常规类型?如果$Diff<$Diff<any, null>, void>
运算符,基本上类似于$Diff
能够在非对象类型上操作。
答案 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