Skip to content

Instantly share code, notes, and snippets.

@qodunpob
Created March 25, 2020 12:30
Show Gist options
  • Save qodunpob/6a156fd6db491cd1b0e5bbc575b97e6a to your computer and use it in GitHub Desktop.
Save qodunpob/6a156fd6db491cd1b0e5bbc575b97e6a to your computer and use it in GitHub Desktop.
TypeScript tricks
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