July 2019
Intermediate to advanced
416 pages
10h 6m
English
Something that goes hand in hand with intersection types and union types are type aliases. Rather than cluttering our code with references to string | number | null, TypeScript gives us the ability to create a handy alias that is expanded out by the compiler into the relevant code.
Suppose that we want to create a type alias that represents the union type of string | number, then we can create an alias that looks as follows:
type StringOrNumber = string | number;
If we revisit our range validation sample, we can change the signature of our function to use this alias, as follows:
class UnionRangeValidationWithTypeAlias extends RangeValidationBase { IsInRange(value : StringOrNumber) : boolean ...Read now
Unlock full access