Created
October 16, 2023 02:32
-
-
Save alexreardon/c41356311e07b15049e99a1570f9cf2c to your computer and use it in GitHub Desktop.
Reimplementing function type helpers
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
function add(a: number, b: number): number { | |
return a + b; | |
} | |
type MyParameters<TFunc extends (...args: any[]) => unknown> = TFunc extends ( | |
...args: infer TArgs | |
) => unknown | |
? TArgs | |
: unknown; | |
type Args = MyParameters<typeof add>; | |
// ^? | |
type MyResultType<TFunc extends (...args: any[]) => unknown> = TFunc extends ( | |
...args: any | |
) => infer TResult | |
? TResult | |
: unknown; | |
type Result = MyResultType<typeof add>; | |
type MyThisParameterType<TFunc extends (this: any, ...args: any[]) => unknown> = | |
TFunc extends (this: infer TThis, ...args: any) => any ? TThis : unknown; | |
type This = MyThisParameterType<typeof add>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment