Skip to content

Instantly share code, notes, and snippets.

@TheodorRene
Created January 20, 2023 08:52
Show Gist options
  • Save TheodorRene/49ec1dfe9cd5aa0fe7f61cb80eed2c3f to your computer and use it in GitHub Desktop.
Save TheodorRene/49ec1dfe9cd5aa0fe7f61cb80eed2c3f to your computer and use it in GitHub Desktop.
Change the type of the given keys
export type ChangeTypeOfKey<T, K extends keyof T, H> = {
[Key in keyof T]: Key extends K ? H : T[Key];
};
type K = {id:string, name:string, date:string}
type KL = ChangeTypeOfKey<K, 'id', string | null>
// ^? {id:string|null, name:string}
type E = {id:string, name:string, date:string}
type EL = ChangeTypeOfKey<K, 'id'|'name', boolean>
// ^? {id:boolean, name:boolean, date:string}