-
Notifications
You must be signed in to change notification settings - Fork 391
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
「重学TS 2.0 」TS 练习题第四十二题 #63
Comments
type IndexOf<
type Arr = [1, 2, 3, 4, 5]; |
// 实现 IndexOf 工具类型,用于获取数组类型中指定项的索引值。若不存在的话,则返回 -1 字面量类型。具体的使用示例如下所示:
type IndexOf<A extends any[], Item, F extends any[] = []> = A extends [infer H, ...infer T]
? H extends Item
? F["length"]
: IndexOf<T, Item, [...F, H]>
: -1;
type Arr = [1, 2, 3, 4, 5];
type I0 = IndexOf<Arr, 0>; // -1
type I1 = IndexOf<Arr, 1>; // 0
type I2 = IndexOf<Arr, 3>; // 2 |
type IsEqual<T, U> = [T] extends [U] ? [U] extends [T] ? true : false : false;
type IndexOf<A extends any[], Item, F extends any[] = []> = A extends [infer H, ...infer T]
? IsEqual<H, Item> extends true
? F["length"]
: IndexOf<T, Item, [...F, H]>
: -1;
type Arr = [1, 2, 3, 4, 5];
type I0 = IndexOf<Arr, 0>; // -1
type I1 = IndexOf<Arr, 1>; // 0
type I2 = IndexOf<Arr, 3>; // 2
type I3 = IndexOf<[1, 3 | 7], 3>; // -1
type I4 = IndexOf<[never, 3 | 7], 3>; // - 注意事项: 构造数组来记录当前迭代到了哪一项,这样匹配到之后就能返回长度,就是索引值。 |
type IndexOf<A extends any[], Item, C extends any[] = []> =
A extends [infer F, ...infer L]
? F extends Item ?
C['length']
: IndexOf<L, Item, [...C, '']>
: -1
type Arr = [1, 2, 3, 4, 5]
type I01 = IndexOf<Arr, 0> // -1
type I11 = IndexOf<Arr, 1> // 0
type I21 = IndexOf<Arr, 3> // 2 |
都要用到数组的length属性,即查找类型 |
type Push<A extends any[], U> = A extends [infer P] ? [P, U] : [U]
type IndexOf<A extends any[], Item extends number, T extends any[] = []> = A extends [infer P1, ...infer P2]
? P1 extends Item
? T['length']
: IndexOf<P2, Item, Push<T, 0>>
: -1
type Arr = [1, 2, 3, 4, 5];
type I0 = IndexOf<Arr, 0>; // -1
type I1 = IndexOf<Arr, 1>; // 0
type I2 = IndexOf<Arr, 3>; // 2 |
// 倒序比较,相等就返回length,否则递归比较,直到为空 type Arr = [1, 2, 3, 4, 5] |
type _IndexOf<A extends any[], Item, Count extends unknown[] = []> = A extends [
infer F,
...infer R
]
? F extends Item
? Count["length"]
: _IndexOf<R, Item, [...Count, 0]>
: -1;
type IndexOf<A extends any[], Item> = _IndexOf<A, Item>; |
// 解法: 增加一个数组, 使用数组长度来指代数组下标求解
type IndexOf<A extends any[], Item, L extends any[] = []> = A extends [f: infer F, ...r: infer R] ? F extends Item ? L['length'] : IndexOf<R, Item, [...L, '']> : -1 |
@jackwangwj 你这个最简洁,但是有个细节不对,A["length"] 应该是是 Rest["length"] |
|
type IndexOf<A extends any[], Item, R extends any[] = []> = A extends [infer S, ...infer B] ?
Item extends S ? R['length'] : IndexOf<B, Item, [1, ...R]>
: -1
type Arr = [1, 2, 3, 4, 5]
type I0 = IndexOf<Arr, 0> // -1
type I1 = IndexOf<Arr, 1> // 0
type I2 = IndexOf<Arr, 3> // 2 |
实现
IndexOf
工具类型,用于获取数组类型中指定项的索引值。若不存在的话,则返回-1
字面量类型。具体的使用示例如下所示:The text was updated successfully, but these errors were encountered: