Is it Possible to extract the name of an Argument in TypeScript?

31 Views Asked by At

Using Parameters I can get the argument types, even as named tuple, but how can I access the name of the argument (or the tuple for that matter), is it possible in the latest TypeScript Version (5.1.6)?

type MyFunction = (argumentName: number) => void

type FirstArgumentType<F extends ((p:any)=>any)> = Parameters<F>[0] 
type FirstArgumentName<F extends ((p:any)=>any)> = ... 

type T0 = Parameters<MyFunction> // [argumentName: number]
type T1 = FirstArgumentType<MyFunction> // number
type T2 = FirstArgumentName<MyFunction> // argumentName

Playground

Essentially I want a type helper that returns argumentName given (argumentName: number) => void, is that possible TypeScript?

I suppose this GitHub Issue is somewhat related, but it gives me no clue what else I could look up https://github.com/microsoft/TypeScript/issues/49122

0

There are 0 best solutions below