September 2022
Intermediate to advanced
410 pages
10h 7m
English
In addition to limiting variables to a set of specific literal values and defining enums, TypeScript allows you to define types that are based on other types, kind of like super-powered generics. These are called mapped types. TypeScript also has a bunch of predefined mapped types that it calls utility types.
So let’s say we have our existing type TicketData:
| | interface TicketData { |
| | id: number |
| | row: number |
| | number: number |
| | status: TicketStatus |
| | } |
And let’s say we have a data source that is unreliable and we want to make all the fields optional for data coming from that source. We could create a new type:
| | interface OptionalTicketData { |
| | id?: number |
| | row?: number |
| | number?: number |
| | status?: TicketStatus ... |
Read now
Unlock full access