代码拉取完成,页面将自动刷新
/*
14 - 第一个元素
-------
by Anthony Fu (@antfu) #简单 #array
### 题目
> 欢迎 PR 改进翻译质量。
实现一个通用`First<T>`,它接受一个数组`T`并返回它的第一个元素的类型。
例如:
```ts
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]
type head1 = First<arr1> // expected to be 'a'
type head2 = First<arr2> // expected to be 3
```
> 在 Github 上查看:https://tsch.js.org/14/zh-CN
*/
/* _____________ 你的代码 _____________ */
type First<T extends any[]> = T extends never[] ? never : T[0]
/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<First<[3, 2, 1]>, 3>>,
Expect<Equal<First<[() => 123, { a: string }]>, () => 123>>,
Expect<Equal<First<[]>, never>>,
Expect<Equal<First<[undefined]>, undefined>>,
]
type errors = [
// @ts-expect-error
First<'notArray'>,
// @ts-expect-error
First<{ 0: 'arrayLike' }>,
]
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。