Flow 中的非空和非未定义类型
Non-null and non-undefined type in Flow
流量定义所谓"Maybe types"。 IE。 ?string
类似于string | null | void
(void
是一种值undefined
)。
除了null
和undefined
之外,是否有类似通用类型的东西可以是任何值?基本上像$Diff<$Diff<any, null>, void>
如果$Diff
运算符能够对非对象类型进行操作。
这里没有一些 "magic" 类型,但像这样的东西应该可以工作:string | number | boolean | {} | []
可以使用 NonMaybeType
Flow 实用程序类型:参见 $NonMaybeType
$NonMaybeType<T>
converts a type T
to a non-maybe type. In other words, the values of $NonMaybeType<T>
are the values of T
except for null and 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
流量定义所谓"Maybe types"。 IE。 ?string
类似于string | null | void
(void
是一种值undefined
)。
除了null
和undefined
之外,是否有类似通用类型的东西可以是任何值?基本上像$Diff<$Diff<any, null>, void>
如果$Diff
运算符能够对非对象类型进行操作。
这里没有一些 "magic" 类型,但像这样的东西应该可以工作:string | number | boolean | {} | []
可以使用 NonMaybeType
Flow 实用程序类型:参见 $NonMaybeType
$NonMaybeType<T>
converts a typeT
to a non-maybe type. In other words, the values of$NonMaybeType<T>
are the values ofT
except for null and 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