ts函数定义的形状几种定义方式

在 TypeScript 中,函数(也称为方法或函数类型)的形状(或称为签名)可以通过多种方式进行定义。以下是几种常见的定义函数形状的方式:

  1. 函数声明

    这是最常见的定义函数的方式,你可以直接在 TypeScript 文件中定义函数。

    复制代码

    typescript复制代码

    |---|-------------------------------------------|
    | | function greet(name: string): string { |
    | | return 'Hello, ' + name; |
    | | } |

  2. 函数表达式

    你也可以使用函数表达式来定义函数,并将其赋值给一个变量。

    复制代码

    typescript复制代码

    |---|---------------------------------------------------|
    | | const greet = function(name: string): string { |
    | | return 'Hello, ' + name; |
    | | }; |

    或者,使用箭头函数:

    复制代码

    typescript复制代码

    |---|----------------------------------------------|
    | | const greet = (name: string): string => { |
    | | return 'Hello, ' + name; |
    | | }; |

  3. 函数类型别名

    使用 type 关键字为函数定义类型别名,这样你就可以在其他地方使用这个类型别名来定义变量或参数。

    复制代码

    typescript复制代码

    |---|---------------------------------------------|
    | | type Greeter = (name: string) => string; |
    | | |
    | | const greet: Greeter = function(name) { |
    | | return 'Hello, ' + name; |
    | | }; |

  4. 接口定义函数形状

    虽然接口通常用于定义对象的形状,但你也可以使用接口来定义函数的形状。然而,这通常不如使用函数类型别名那么常见。

    复制代码

    typescript复制代码

    |---|-----------------------------------------------------|
    | | interface GreeterInterface { |
    | | (name: string): string; |
    | | } |
    | | |
    | | const greet: GreeterInterface = function(name) { |
    | | return 'Hello, ' + name; |
    | | }; |

  5. 类型断言

    在某些情况下,你可能需要将一个值断言为特定的函数类型,尽管 TypeScript 编译器可能无法验证该断言的准确性。

    复制代码

    typescript复制代码

    |---|----------------------------------------|
    | | const greet = function(name: any) { |
    | | return 'Hello, ' + name; |
    | | } as (name: string) => string; |

  6. 可选参数和默认参数

    在函数定义中,你可以使用 ? 来表示某个参数是可选的,或者使用 = 来为参数提供默认值。

    复制代码

    typescript复制代码

    |---|-----------------------------------------------------------------------------|
    | | function greet(name: string, age?: number, greeting = 'Hello'): string { |
    | | return greeting + ', ' + name + (age ? ' (' + age + ')' : ''); |
    | | } |

  7. 剩余参数

    使用 ...args 的语法来定义一个函数,该函数可以接收任意数量的参数,并将它们作为数组处理。

    复制代码

    typescript复制代码

    |---|-------------------------------------------------------|
    | | function sum(...numbers: number[]): number { |
    | | return numbers.reduce((acc, val) => acc + val, 0); |
    | | } |

  8. 重载

    在 TypeScript 中,你可以使用重载来定义多个具有相同名称但参数列表不同的函数。

    复制代码

    typescript复制代码

    |---|------------------------------------------------------------------------|
    | | function greet(name: string): string; |
    | | function greet(age: number, name: string): string; |
    | | function greet(nameOrAge: string | number, name?: string): string { |
    | | if (typeof nameOrAge === 'string') { |
    | | return `Hello, ${nameOrAge}`; |
    | | } else { |
    | | return `Hello, ${name} who is ${nameOrAge} years old`; |
    | | } |
    | | } |

以上就是在 TypeScript 中定义函数形状的几种常见方式。根据具体的应用场景和需求,你可以选择最适合你的方式。

相关推荐
vibecoding日记2 天前
为什么我就想要「线性历史 + Signed Commits」,GitHub 却把我当猴耍 🤬🎙️
git·编程工具
程序员小崔日记3 天前
如何将代码轻松上传到 Gitee?Git 使用全攻略!
git·gitee·上传
Bigger4 天前
为什么你的 Git 提交需要签名?—— Git Commit Signing 完全指南
git·开源·github
DianSan_ERP4 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
红豆子不相思5 天前
Tomcat 环境搭建与集群实战
服务器·git·tomcat
杰哥技术分享5 天前
Git 仓库迁移技术文档:从 CODING.net 迁移至腾讯云 CNB
git
梅孔立5 天前
Ansible 100 台服务器一键管控实战 进阶版
服务器·git·ansible
qq_426003965 天前
git切换当前分支到远程分支
git
ON10N5 天前
100% 纯 Vibe Coding,我是怎么用 AI 撸出一个 VS Code 插件的
git·ai编程·visual studio code