Skip to content

Instantly share code, notes, and snippets.

@alexreardon
Created October 16, 2023 02:32
Show Gist options
  • Save alexreardon/c41356311e07b15049e99a1570f9cf2c to your computer and use it in GitHub Desktop.
Save alexreardon/c41356311e07b15049e99a1570f9cf2c to your computer and use it in GitHub Desktop.
Reimplementing function type helpers
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