600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > TypeScript 函数类型参数的用法举例

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

时间:2019-12-18 09:58:23

相关推荐

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

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 的类型。

编译错误:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。