Created
August 6, 2021 11:54
-
-
Save karol-majewski/408644df3fe8c1a5a20289e645c3c6c7 to your computer and use it in GitHub Desktop.
Extract methods/function properties from a type in TypeScript
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 AnyFunction = (...args: any[]) => any; | |
type FunctionPropertyOf<T> = { | |
[P in keyof T]: T[P] extends AnyFunction ? P : never; | |
}[keyof T]; | |
type MethodOf<T> = { | |
[P in keyof T]: T[P] extends (this: ThisType<T[P]>, ...args: any[]) => any ? P : never; | |
}[keyof T]; | |
type Foo = MethodOf<RemoveIndexSignature<Storage>> // "clear" | "getItem" | "key" | "removeItem" | "setItem" | |
type Bar = FunctionPropertyOf<RemoveIndexSignature<Storage>> // "clear" | "getItem" | "key" | "removeItem" | "setItem" | |
/** | |
* https://stackoverflow.com/a/66252656/10325032 | |
*/ | |
type RemoveIndexSignature<T> = { | |
[P in keyof T as string extends P ? never : number extends P ? never : P]: T[P]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment