Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############################
- # USE TYPE AS KEY OF OBJECT#
- ############################
- type DayOfTheWeek = "sunday" | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday";
- type ChoresMap = { [day in DayOfTheWeek]: string };
- const chores: ChoresMap = { // ERROR! Property 'saturday' is missing in type '...'
- "sunday": "do the dishes",
- "monday": "walk the dog",
- "tuesday": "water the plants",
- "wednesday": "take out the trash",
- "thursday": "clean your room",
- "friday": "mow the lawn",
- };
- ##########################################
- # USE TYPE AS KEY OF OBJECT with GENERICS#
- ##########################################
- type DayOfTheWeek = "sunday" | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday";
- type DayOfTheWeekMap<T> = { [day in DayOfTheWeek]: T };
- const chores: DayOfTheWeekMap<string> = {
- "sunday": "do the dishes",
- "monday": "walk the dog",
- "tuesday": "water the plants",
- "wednesday": "take out the trash",
- "thursday": "clean your room",
- "friday": "mow the lawn",
- "saturday": "relax",
- };
- const workDays: DayOfTheWeekMap<boolean> = {
- "sunday": false,
- "monday": true,
- "tuesday": true,
- "wednesday": true,
- "thursday": true,
- "friday": true,
- "saturday": false,
- }
- #########################
- #EXCLUDE ITEMS FROM TYPE#
- #########################
- interface CreditCardOwnerInformationUIDTO {
- floor: string;
- department: string;
- postalCode: string;
- }
- type TGetValues = Pick<
- CreditCardOwnerInformationUIDTO,
- Exclude<keyof CreditCardOwnerInformationUIDTO, 'floor' | 'department'>
- >;
- // TGetValues should be = postalCode: string;
- ##################################
- # UNION TYPE BASED ON OBJECT KEYS#
- ##################################
- const paymentTypes = {
- prepaid: 'foo',
- at_destination: 'bar',
- advance_payment: 'foo bar',
- };
- type p = keyof typeof paymentTypes;
- // P must be = "prepaid" | "at_destination" | "advance_payment";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement