Skip to content

Instantly share code, notes, and snippets.

@arbaz52
Created September 12, 2021 20:16
Show Gist options
  • Save arbaz52/b68bf1dc2a0e5383cc0fb92cbc82c157 to your computer and use it in GitHub Desktop.
Save arbaz52/b68bf1dc2a0e5383cc0fb92cbc82c157 to your computer and use it in GitHub Desktop.
Typescript Custom Type Modifiers
export type ModifyKey<I, K extends keyof I, V> = Omit<I, K> &
{ [K in keyof I]: V };
export type PickAndModifyKey<I, K extends keyof I, V> = ModifyKey<
Pick<I, K>,
K,
V
>;
// key should be an interface/object
export type PickApartKey<
I,
K extends keyof I,
P extends keyof I[K]
> = PickAndModifyKey<I, K, Pick<I[K], P>>;
type Unboxed<T> = T extends (infer U)[] ? U : T;
export type PickApartArrayKey<
I, // interface
K extends keyof I, //key(array) which needs to be changed
P extends keyof Unboxed<I[K]> //keys of K that are needed
> = PickAndModifyKey<I, K, Pick<Unboxed<I[K]>, P>[]>;