Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get function parameter names and types in TypeScript

Tags:

typescript

I've played around with the Parameters<T> type in TypeScript, which returns a tuple with the parameter types. Now I wonder if there's a way to not just return the types of the paramters but also the names?

Something like:

function MyFunc(a: string, b: boolean, ...args: string[]) {}

type extractedType = ParametersWithName<typeof MyFunc>

Where extractedType results ends up as:

{
    a: string, 
    b: string
}

I would use that to convert from regular function parameters to functions that receive a parameter object (like React props).

like image 797
asgerhallas Avatar asked Sep 18 '19 12:09

asgerhallas


People also ask

What is a parameter type of a TypeScript function?

In functions, parameters are the values or arguments that passed to a function. The TypeScript, compiler accepts the same number and type of arguments as defined in the function signature. If the compiler does not match the same parameter as in the function signature, then it will give the compilation error.

How do you find a parameter type?

You can access the type of a specific parameter by using square brackets, in the same way you would access an array element at index. Here is an example of how you would use the Parameters utility type for 2 functions that take the same object as a parameter.

What is the type of a function in TypeScript?

Types of Functions in TypeScript: There are two types of functions in TypeScript: Named Function. Anonymous Function.

How TypeScript check types?

Using TypeScript type guards Checking a specific value's type at runtime is the primary function of type guards. This helps the TypeScript compiler, which then uses the information to become more predictive about the types. The above code snippet showcases the example of the type guard instanceof .


1 Answers

For better or worse, parameter names are not part of a function's type. For example, the type signatures (foo: string) => void and (bar: string) => void are completely identical as far as type compatibility is concerned. The names foo and bar are (usually) preserved by IntelliSense as a form of documentation to help developers, but they are essentially implementation details. And since there isn't supposed to be a way to distinguish two function types that differ only by parameter names, there's no mechanism provided to convert these names into string literal types for use as object types.

The ability to convert between function parameter lists and tuple types was introduced in TypeScript 3.0 via microsoft/TypeScript#24897. This is what makes Parameters<> possible. That pull request has the following remark about parameter names:

Note that when a tuple type is inferred from a sequence of parameters and later expanded into a parameter list, ... the original parameter names are used in the expansion (however, the names have no semantic meaning and are not otherwise observable).

So there's definitely no way to use Parameters<> or something like it to pull parameter names out of a function and use them.

Sorry I don't have a better answer for you. Hope that helps anyway; good luck!

like image 161
jcalz Avatar answered Oct 22 '22 04:10

jcalz