Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 如何在TS中键入函数签名?_Typescript - Fatal编程技术网

Typescript 如何在TS中键入函数签名?

Typescript 如何在TS中键入函数签名?,typescript,Typescript,如何为一个函数键入参数以便它们可以重用,例如,我有一个签名为x,y的函数。我想重复使用类似函数的类型签名。在TS中可能吗 function add(x: number, y: number): number { return x + y; } 是的,您可以创建自定义类型并重用它: type SomeFuncType = (x: number, y: number) => number; const add: SomeFuncType = (a, b) => a + b;

如何为一个函数键入参数以便它们可以重用,例如,我有一个签名为x,y的函数。我想重复使用类似函数的类型签名。在TS中可能吗

function add(x: number, y: number): number {
    return x + y;
}

是的,您可以创建自定义类型并重用它:

type SomeFuncType = (x: number, y: number) => number;

const add: SomeFuncType = (a, b) => a + b;
const mul: SomeFuncType = (a, b) => a * b;

console.log(add(1, 3));
console.log(mul(1, 3));

是的,您可以创建自定义类型并重用它:

type SomeFuncType = (x: number, y: number) => number;

const add: SomeFuncType = (a, b) => a + b;
const mul: SomeFuncType = (a, b) => a * b;

console.log(add(1, 3));
console.log(mul(1, 3));

你是说Fn=x:number,y:number=>number;常数加:Fn=x,y=>x+y;常数sub:Fn=x,y=>x-y?你是说Fn=x:number,y:number=>number;常数加:Fn=x,y=>x+y;常数sub:Fn=x,y=>x-y?谢谢,我有一个类似的问题,也许你也可以帮我。谢谢,我有一个类似的问题,也许你也可以帮我。谢谢