Created
September 12, 2021 20:16
-
-
Save arbaz52/b68bf1dc2a0e5383cc0fb92cbc82c157 to your computer and use it in GitHub Desktop.
Typescript Custom Type Modifiers
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
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>[]>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/60067100/why-is-the-infer-keyword-needed-in-typescript
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html