Learning TypeScript 2.x
上QQ阅读APP看书,第一时间看更新

Type aliases

TypeScript allows us to declare type aliases by using the type keyword:

type PrimitiveArray = Array<string|number|boolean>; 
type MyNumber = number;  
type Callback = () => void

Type aliases are exactly the same as their original types; they are simply alternative names. Type aliases can help us to make our code more readable, but it can also lead to some problems.

If you work as part of a large team, the indiscriminate creation of aliases can lead to maintainability problems. The book Maintainable JavaScript, Nicholas C. Zakas recommends that you "avoid modifying the objects you don't own". Nicholas was talking about adding, removing, or overriding methods in objects that have not been declared by you (DOM objects, BOM objects, primitive types, and third-party libraries), but we can apply this rule to the use of aliases as well.