SoFunction
Updated on 2025-02-28

TypeScript Get the parameter type, return value type and define the return function type of the function

case:

function test(lzwme: string, idx: number) {
  return {
    lzwme,
    idx,
  };
}

1 Get the parameter type of the function

Use predefinedParametersYou can get a list of parameter types for a function.
GettestFunction parameter type:

type TestArgsType = Parameters<typeof test>;
// TestArgsType => [lzwme: string, idx: number]

GetidxParameter type:

type TestArgsType = Parameters<typeof test>[1];
// TestArgsType => idx: number

Let's take a lookParametersDefinition of  :

type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;

We can see that it is mainly throughinfer PObtainedTList of parameter typesPand return ifTIf it is not a function, it will returnnever

2 Get the return value type of the function

Use predefinedReturnTypeYou can get a list of parameter types for a function.

GettestThe return value type of the function:

type TestReturnType = ReturnType<typeof test>;

Let's take a lookReturnTypeDefinition of  :

type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;

andParametersVery similar, the difference is throughinfer RGet and returnTThe return value type.

TypeScript definition returns function type

General function declaration:

//The first formlet c: Function;
c = function(): void {
('It work');
}

//The second formfunction test(): Function{
return function():void{
('it work');
}
}

let a:Function = test();
a();

//The third form, arrow functionlet d: (para: string) =&gt; string;
d = function(param: string): string {
return param;
}

//The fourth form, type alias, arrow functiontype e = (para: string) =&gt; string;
const f:e = function(pass: string): string{

return pass;

}

//The fifth form, interfaceinterface g{
(para: string): string;
}
const h: g = function(pass: string): string{
return pass;
}

Arrow function:

In fact, this is just a simple declaration definition involved.

As:

let myAdd: (x:number, y:number) => number =
function(x: number, y: number): number { return x + y; };

It's just a function type declaration (or anonymous function). If we define it with a simple variable declaration, the complete format is as follows:

let x: number = 10;

The one on itnumberPart of it is equivalent to the beginning(x:number, y:number) => number, this part is a type (or function type), just a definition; even if you use:

let myAdd: (aaaaaaaaaaaaaaaaaaaaaa:number, bbbbbbbbbbbbbbbbbbbbbbbb:number) => number =
function(x: number, y: number): number { return x + y; };

It's OK, too.

Similarly, the declaration definition of a variable can be like this:

let x = 10;

This is naturally attributed to TS' automatic derivation ability. Therefore, as above, it can also be simplified to:

const myAdd = (x: number, y: number) =&gt; x + y;

=The first part of  is omitted and automatically deduced by ts; then, it is an actual anonymous function writing method.

The above is the detailed content of TypeScript obtaining the parameter type, return value type and definition of return function type. For more information about TypeScript obtaining the parameter type, return value type and definition of return function type, please pay attention to my other related articles!