Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14 - 第一个元素 #33290

Open
Skytim opened this issue May 19, 2024 · 0 comments
Open

14 - 第一个元素 #33290

Skytim opened this issue May 19, 2024 · 0 comments
Labels
14 answer Share answers/solutions to a question zh-CN 简体中文

Comments

@Skytim
Copy link

Skytim commented May 19, 2024

type First<T extends any[]> = T extends []? never: T[0]
  1. T extends []:这部分表达式是一个条件类型,用来检查 T 是否可以赋值给 [],即 T 是否是一个空数组。在 TypeScript 中,extends 关键字在泛型中用于表示类型约束,在这里用来评估条件。
  2. never:这是 TypeScript 中的一种类型,表示永不发生的值。例如,一个抛出异常或永不返回的函数会有 never 的返回类型。这里用它来表示当数组为空时,没有可返回的类型。
  3. T[0]:这表示访问数组 T 的第一个元素的类型。如果 T 是如 number[] 这样的数组类型,则 T[0] 会是 number。
@Skytim Skytim added answer Share answers/solutions to a question zh-CN 简体中文 labels May 19, 2024
@github-actions github-actions bot added the 14 label May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
14 answer Share answers/solutions to a question zh-CN 简体中文
Projects
None yet
Development

No branches or pull requests

1 participant