We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
题目链接:FirstOfArray
实现一个 FirstOfArray 满足测试用例 cases,作用是返回数组中的第一项
FirstOfArray
cases
[3, 2, 1]
3
[() => 123, { a: string }]
() => 123
[]
never
[undefined]
undefined
import { Equal, Expect } from "@type-challenges/utils"; type cases = [ Expect<Equal<FirstOfArray<[3, 2, 1]>, 3>>, Expect<Equal<FirstOfArray<[() => 123, { a: string }]>, () => 123>>, Expect<Equal<FirstOfArray<[]>, never>>, Expect<Equal<FirstOfArray<[undefined]>, undefined>> ]; type errors = [ // @ts-expect-error FirstOfArray<"notArray">, // @ts-expect-error FirstOfArray<{ 0: "arrayLike" }> ];
type FirstOfArray<Arr extends any[]> = Arr extends [infer Item, ...any[]] ? Item : never;
Arr
infer Item
Item
[infer Item, ...any[]]
extends
[] extends [infer Item, ...any[]]
false
true
type FirstOfArray<Arr extends any[]> = Arr extends [] ? never : Arr[0];
[] extens []
Arr[0]
type FirstOfArray<Arr extends any[]> = "0" extends keyof Arr ? Arr[0] : never;
keyof Arr
key
"0" extends keyof Arr
type FirstOfArray<Arr extends any[]> = Arr["length"] extends 0 ? never : Arr[0];
Arr["length"]
Arr["length"] extends 0
0
type FirstOrArray<Arr extends any[]> = Arr[number] extends never ? never : Arr[0];
Arr[number] 可以得到数组的每一项值的联合类型
Arr[number]
type A1<Arr> = Arr[number] type A2: A1<[1, 2, 3]> // => 1 | 2 | 3 type A3: A1<[]> // => never
如果是个空数组 Arr<[]> 会返回 never,通过判断 Arr[number] extends never 是否为 never
Arr<[]>
Arr[number] extends never
如果为 never 输出 never 否则输出 Arr[0]
type FirstOfArray<Arr extends any[]> = Arr[Extract<keyof Arr, "0">];
空数组的几种判断方法
T extends []
T extends never[]
"0" extends keyof T
T["length"] extends 0
T[number] extends never
The text was updated successfully, but these errors were encountered:
No branches or pull requests
题目
题目链接:FirstOfArray
实现一个
FirstOfArray
满足测试用例cases
,作用是返回数组中的第一项[3, 2, 1]
结果为3
[() => 123, { a: string }]
结果为() => 123
[]
结果为never
[undefined]
结果为undefined
答案
方法一
知识点
Arr
继承自任意类型的数组infer Item
是声明一个Item
,类似于变量声明,在后面都可以使用[infer Item, ...any[]]
取出数组的第一项,放在变量Item
extends
判断泛型Arr
是否继承自[infer Item, ...any[]]
[] extends [infer Item, ...any[]]
结果为false
,所以输出为never
true
,输出数组第一项方法二
知识点
[] extens []
为true
输出为never
false
输出为Arr[0]
方法三
知识点
keyof Arr
取出数组中的每一个key
"0" extends keyof Arr
判断数组中有没第一项,因为空数组是没有第一项的。Arr[0]
否则为never
方法四
知识点
Arr["length"]
可以获取泛型Arr
的长度Arr["length"] extends 0
判断数组长度是否为0
0
输出never
,否者输出Arr[0]
方法五
知识点
Arr[number]
可以得到数组的每一项值的联合类型如果是个空数组
Arr<[]>
会返回never
,通过判断Arr[number] extends never
是否为never
如果为
never
输出never
否则输出Arr[0]
方法六
总结
空数组的几种判断方法
T extends []
T extends never[]
"0" extends keyof T
T["length"] extends 0
T[number] extends never
基础知识
The text was updated successfully, but these errors were encountered: