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 的类型。
编译错误:
相关文章
- [Typescript] Declaring Globals for a Library
- [Typescript] Step6: Types at Runtime
- [Typescript Kaop-ts] Use AOP in Vue Components with TypeScript and Kaop-ts
- [TypeScript] Union Types and Type Aliases in TypeScript
- [Typescript] 78. Medium - Unqiue
- [Typescript] 71. Medium - Trunc
- [Typescript] Tips: Create autocomplete helper which allows for arbitrary values
- [Typescript challenge] 19. Medium - Tuple to Union
- [TypeScript] Understanding Generics with RxJS
- TypeScript里的可选链(Optional Chaining,即问号)用法 - ?.
- ng serve出错,关于TypeScript的版本问题