February 2019
Beginner
694 pages
18h 4m
English
Sometimes, when using union types, it can be difficult to remember what types are allowed. To cater for this, TypeScript introduces the concept of a type alias, where we can create a special named type for a type union. A type alias is, therefore, a convenient naming convention for union types. Type aliases can be used wherever normal types are used, and are denoted by using the type keyword. We can, therefore, simplify the use of union types in our code as follows:
type StringOrNumber = string | number;
function addWithAlias(
arg1 : StringOrNumber,
arg2 : StringOrNumber
) {
return arg1.toString() + arg2.toString();
}
Here, we have defined a type alias, named StringOrNumber, which is a union type that can be either a string ...
Read now
Unlock full access