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, ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.