上QQ阅读APP看书,第一时间看更新
Type aliases
When your types become more complex or when you want to reuse the same definition in several places, you can create a type alias:
// Source file: src/types_advanced.js
type simpleFlag = number | boolean;
type complexObject = {
id: string,
name: string,
indicator: simpleFlag,
listOfValues: Array<number>
};
After defining types in this fashion, you can just use them anywhere, even in the definition of new types, as we did in complexObject, where we defined a field to be of the previously defined simpleFlag type:
// Source file: src/types_advanced.js
let myFlag: simpleFlag;
let something: complexObject = {
id: "B2209",
name: "Anna Malli",
indicator: 1,
listOfValues: [12, 4, 56]
};
Type aliases can even be generic, as we'll see in the next section. You can also export types from a module, and import them for usage anywhere; we'll get to that in the Working with libraries section.