IndexOf
介绍
实现数组的IndexOf类型, indexOf<T,U>接收数组T, 任意U, 返回U在T中第一次出现的索引
ts
type Res = IndexOf<[1, 2, 3], 2>; // expected to be 1type Res1 = IndexOf<[2,6, 3,8,4,1,7, 3,9], 3>; // expected to be 2type Res2 = IndexOf<[0, 0, 0], 2>; // expected to be -1
View on GitHubts
type Res = IndexOf<[1, 2, 3], 2>; // expected to be 1type Res1 = IndexOf<[2,6, 3,8,4,1,7, 3,9], 3>; // expected to be 2type Res2 = IndexOf<[0, 0, 0], 2>; // expected to be -1
起点
ts
/* _____________ Your Code Here _____________ */typeIndexOf <T ,U > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeIndexOf <T ,U > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
typeIndexOf <T extends any[],U ,X extends number = number> =U extendsT [number]? {[X in keyofT asX extends `${number}` ? (T [X ] extendsU ?U : never) : never ] :X }[U ]: -1
ts
typeIndexOf <T extends any[],U ,X extends number = number> =U extendsT [number]? {[X in keyofT asX extends `${number}` ? (T [X ] extendsU ?U : never) : never ] :X }[U ]: -1