Last active
December 9, 2019 17:48
-
-
Save valentingavela/e59ac8034012e1009e43d6f8c0555073 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################ | |
# 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"; | |
################################## | |
# MAKE ALL PROPERTIES OPTIONAL # | |
################################## | |
type Optionals<T> = { | |
[P in keyof T]?: T[P]; | |
}; | |
################################## | |
##### MORE MAGIC ################# | |
################################## | |
https://stackoverflow.com/questions/13315131/enforcing-the-type-of-the-indexed-members-of-a-typescript-object | |
export interface StringTMap<T> { [key: string]: T; }; | |
export interface NumberTMap<T> { [key: number]: T; }; | |
export interface StringAnyMap extends StringTMap<any> {}; | |
export interface NumberAnyMap extends NumberTMap<any> {}; | |
export interface StringStringMap extends StringTMap<string> {}; | |
export interface NumberStringMap extends NumberTMap<string> {}; | |
export interface StringNumberMap extends StringTMap<number> {}; | |
export interface NumberNumberMap extends NumberTMap<number> {}; | |
export interface StringBooleanMap extends StringTMap<boolean> {}; | |
export interface NumberBooleanMap extends NumberTMap<boolean> {}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment