Created
March 25, 2020 12:30
-
-
Save qodunpob/6a156fd6db491cd1b0e5bbc575b97e6a to your computer and use it in GitHub Desktop.
TypeScript tricks
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
type OptionalOf<T> = { [K in keyof T]?: T[K] } | |
function typedMerge<T extends object>(target: T, ...sources: OptionalOf<T>[]): T { | |
return Object.assign(target, ...sources) | |
} | |
interface IUser { | |
name: string; | |
age: number; | |
hobbie: HOBBIES; | |
} | |
enum HOBBIES { | |
PLAY_FOOTBOOL = 'PLAY_FOOTBOOL', | |
WATCH_TV = 'WATCH_TV' | |
} | |
const user: IUser = { | |
name: 'John', | |
age: 28, | |
hobbie: HOBBIES.PLAY_FOOTBOOL | |
} | |
typedMerge(user, { | |
age: 35, | |
hobbie: HOBBIES.WATCH_TV, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment