
/*
898 - Includes
-------
by null (@kynefuk) #쉬움 #array
### 질문
JavaScript의 `Array.includes` 함수를 타입 시스템에서 구현하세요. 타입은 두 인수를 받고, `true` 또는 `false`를 반환해야 합니다.
예시:
type isPillarMen = Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'> // expected to be `false`
> GitHub에서 보기: https://tsch.js.org/898/ko
*/
/* _____________ 여기에 코드 입력 _____________ */
// 예시 코드
// type Includes<T extends readonly any[], U> = any
type Includes<T extends readonly any[], U> = T extends [infer F, ...infer R]
? Equal<F, U> extends true
? true
: Includes<R, U>
: false;
/* _____________ 테스트 케이스 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Kars'>, true>>,
Expect<Equal<Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'>, false>>,
Expect<Equal<Includes<[1, 2, 3, 5, 6, 7], 7>, true>>,
Expect<Equal<Includes<[1, 2, 3, 5, 6, 7], 4>, false>>,
Expect<Equal<Includes<[1, 2, 3], 2>, true>>,
Expect<Equal<Includes<[1, 2, 3], 1>, true>>,
Expect<Equal<Includes<[{}], { a: 'A' }>, false>>,
Expect<Equal<Includes<[boolean, 2, 3, 5, 6, 7], false>, false>>,
Expect<Equal<Includes<[true, 2, 3, 5, 6, 7], boolean>, false>>,
Expect<Equal<Includes<[false, 2, 3, 5, 6, 7], false>, true>>,
Expect<Equal<Includes<[{ a: 'A' }], { readonly a: 'A' }>, false>>,
Expect<Equal<Includes<[{ readonly a: 'A' }], { a: 'A' }>, false>>,
Expect<Equal<Includes<[1], 1 | 2>, false>>,
Expect<Equal<Includes<[1 | 2], 1>, false>>,
Expect<Equal<Includes<[null], undefined>, false>>,
Expect<Equal<Includes<[undefined], null>, false>>,
]
/* _____________ 다음 단계 _____________ */
/*
> 정답 공유하기: https://tsch.js.org/898/answer/ko
> 정답 보기: https://tsch.js.org/898/solutions
> 다른 문제들: https://tsch.js.org/ko
*/
Equal<F, U> extends true ? : 첫 번째 원소 F와 U가 동일한지 확인하고 만약 true를 반환한다면
true : Includes<O, U> : false; : true를 반환하고, F와 U가 다르면 Include<O, U>를 실행하고, 그것도 아니라면 false를 반환한다.
type Includes<T extends readonly any[], U> = T extends [infer F, ...infer R] ?
... : ...
type Includes<T extends readonly any[], U> = T extends [infer F, ...infer R] ?
Equal<F, U> extends true ? true : ... : ...
type Includes<T extends readonly any[], U> = T extends [infer F, ...infer R]
? Equal<F, U> extends true
? true
: Includes<R, U>
: false;
출처