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 的类型。
编译错误: