타입 버전의 Array.indexOf를 구현하라.
indexOf<T, U>는 배열 T와 any 형태의 U를 받고, T에 있는 첫번째 인덱스를 반환한다
Implement the type version of Array.indexOf, indexOf<T, U> takes an Array T, any U and returns the index of the first U in Array T.
import type { Equal } from '@type-challenges/utils'
type IndexOfImplement<T extends any[],U,Index extends any[]=[]>=
T['length'] extends Index['length']?-1:
Equal<T[Index['length']],U> extends true?
Index['length']
:IndexOfImplement<T,U,[...Index,1]>
type IndexOf<T extends any[], U> = IndexOfImplement<T,U>
우선 사용처에서 구현용 제네릭을 사용하지 못하게 하기 위해 구현과 실제 타입을 분리했다.
만약 해당 배열안에 U가 없으면 -1을 리턴을 한다.
T[Index]와 U가 같은지 확인하기 위해 type challenge의 Equal 커스텀 타입을 사용했다.
다른 사람들도 eqaul을 사용해 풀이를 한 것으로 보인다.