TypeScript 函数类型参数的用法举例
生活随笔
收集整理的這篇文章主要介紹了
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 的類型。
編譯錯誤:
總結
以上是生活随笔為你收集整理的TypeScript 函数类型参数的用法举例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 宏基因组扩增子图表解读2散点图:组间整体
- 下一篇: TypeScript 里 interfa