跳到主要内容

IndexOf

介绍

实现数组的IndexOf类型, indexOf<T,U>接收数组T, 任意U, 返回U在T中第一次出现的索引

ts
type Res = IndexOf<[1, 2, 3], 2>; // expected to be 1
type Res1 = IndexOf<[2,6, 3,8,4,1,7, 3,9], 3>; // expected to be 2
type Res2 = IndexOf<[0, 0, 0], 2>; // expected to be -1
ts
type Res = IndexOf<[1, 2, 3], 2>; // expected to be 1
type Res1 = IndexOf<[2,6, 3,8,4,1,7, 3,9], 3>; // expected to be 2
type Res2 = IndexOf<[0, 0, 0], 2>; // expected to be -1
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type IndexOf<T, U> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<IndexOf<[1, 2, 3], 2>, 1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IndexOf<[2, 6, 3, 8, 4, 1, 7, 3, 9], 3>, 2>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IndexOf<[0, 0, 0], 2>, -1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IndexOf<[string, 1, number, 'a'], number>, 2>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IndexOf<[string, 1, number, 'a', any], any>, 4>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IndexOf<[string, 'a'], 'a'>, 1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IndexOf<[any, 1], 1>, 1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
ts
/* _____________ Your Code Here _____________ */
 
type IndexOf<T, U> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<IndexOf<[1, 2, 3], 2>, 1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IndexOf<[2, 6, 3, 8, 4, 1, 7, 3, 9], 3>, 2>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IndexOf<[0, 0, 0], 2>, -1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IndexOf<[string, 1, number, 'a'], number>, 2>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IndexOf<[string, 1, number, 'a', any], any>, 4>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IndexOf<[string, 'a'], 'a'>, 1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IndexOf<[any, 1], 1>, 1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
type IndexOf<T extends any[],U, X extends number = number> = U extends T[number]
? {[X in keyof T as X extends `${number}` ? (T[X] extends U ? U : never) : never ] : X}[U]
: -1
 
ts
type IndexOf<T extends any[],U, X extends number = number> = U extends T[number]
? {[X in keyof T as X extends `${number}` ? (T[X] extends U ? U : never) : never ] : X}[U]
: -1
 
view more solutions