zl程序教程

您现在的位置是:首页 >  前端

当前栏目

TypeScript 函数类型参数的用法举例

typescript 函数 类型 用法 参数 举例
2023-09-14 09:02:58 时间
export type GeneralFunction<T,V> = {
    (name: T, value: V): T
}

四种不同的写法:

const a1: GeneralFunction<string, number> = (a: string, b: number) => a + b;

const a2: GeneralFunction<string, number> = (a, b) => a + b;

const a3 = (a: string, b: number) => a + b;

const a4 = <GeneralFunction<string, number>>((a:string,b:number) => a + b);

console.log(a1('Ethan', 1));
console.log(a2('Ethan', 2));
console.log(a3('Ethan', 3));
console.log(a4('Ethan', 4));

其中第三种其实并没有用到 GeneralFunction 的类型。

编译错误:
在这里插入图片描述