类型别名(Type Aliases)
如果你想要在多个地方重复使用复杂的类型,则可以在Flow中使用类型别名将它们别名。
// @flow
type MyObject = {
foo: number,
bar: boolean,
baz: string,
};
var val: MyObject = { /* ... */ };
function method(val: MyObject) { /* ... */ }
class Foo { constructor(val: MyObject) { /* ... */ } }
类型别名语法(Type Alias Syntax )
type Alias = Type;
任何类型都可以出现在一个类型别名中。
type NumberAlias = number;
type ObjectAlias = {
property: string,
method(): number,
};
type UnionAlias = 1 | 2 | 3;
type AliasAlias = ObjectAlias;
类型别名范型(Type Alias Generics)
类型别名也可以有自己的泛型。
type MyObject<A, B, C> = {
property: A,
method(val: B): C,
};
类型别名泛型被参数化。当你使用类型别名时,你需要为每个泛型传递参数。
// @flow
type MyObject<A, B, C> = {
foo: A,
bar: B,
baz: C,
};
var val: MyObject<number, boolean, string> = {
foo: 1,
bar: true,
baz: 'three',
};