Skip to main content

Command Palette

Search for a command to run...

[ts-challenges] Includes / Push / Unshift / Parameters

Published
3 min read

Includes

설계 의도

  • 튜플 타입 T를 첫 번째 요소인 Head와 나머지 요소들인 Tail로 분리해서 추론한다.

  • Head가 제네릭 U 타입과 동일하지 않으면 Includes 타입을 Tail에 대해 재귀적으로 다시 실행하면서 다음 요소 검사하기

  • boolean 타입의 경우, truefalse이 모두 할당가능하니까 엄격하게 동등한지 비교하기 위해서 Equal 헬퍼함수 사용

코드

type Equal<X, Y> =
  (<T>() => T extends X ? 1 : 2) extends
  (<T>() => T extends Y ? 1 : 2) ? true : false;
/* _____________ Your Code Here _____________ */

type Includes<T extends readonly any[], U> = T extends [infer Head,...infer Tail] ? Equal<Head,U> extends true ? true : Includes<Tail,U> :  false


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>>,
]

Push

설계의도

  • 기존 튜플 타입의 맨 마지막에 새로운 요소 추가하도록 처리

코드


/* _____________ Your Code Here _____________ */

type Push<T extends any[], U> = [...T, U]

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Push<[], 1>, [1]>>,
  Expect<Equal<Push<[1, 2], '3'>, [1, 2, '3']>>,
  Expect<Equal<Push<['1', 2, '3'], boolean>, ['1', 2, '3', boolean]>>,
]

Unshift

설계의도

  • Push와 반대로 새로운 요소를 튜플의 앞에 추가하도록 처리

코드

/* _____________ Your Code Here _____________ */

type Unshift<T extends any[], U> = [U, ...T]

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Unshift<[], 1>, [1]>>,
  Expect<Equal<Unshift<[1, 2], 0>, [0, 1, 2]>>,
  Expect<Equal<Unshift<['1', 2, '3'], boolean>, [boolean, '1', 2, '3']>>,
]

Parameters

설계의도

  • 제네릭 T타입이 함수 시그니처 형태인 경우, 조건부 타입 내부에서 매개변수의 타입을 infer P를 사용해서 P라는 제네릭 타입으로 추론해서 리턴하도록 처리

  • infer은 여러 타입을 자동으로 하나의 튜플타입으로 정리해준다

코드

/* _____________ Your Code Here _____________ */

type MyParameters<T extends (...args: any[]) => any> = T extends (...args: infer P) => any ? P : never;

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

function foo(arg1: string, arg2: number): void {}
function bar(arg1: boolean, arg2: { a: 'A' }): void {}
function baz(): void {}

type cases = [
  Expect<Equal<MyParameters<typeof foo>, [string, number]>>,
  Expect<Equal<MyParameters<typeof bar>, [boolean, { a: 'A' }]>>,
  Expect<Equal<MyParameters<typeof baz>, []>>,
]

More from this blog

Conductor 워크스페이스마다 .env 파일 자동 복사하기

문제를 만나다 개인 프로젝트를 진행할 때 기능을 병렬로 구현하기 위해서 Conductor를 사용하고 있다. 여러 개의 워크 스페이스를 새로 만들 때마다 환경변수를 매번 수동 복사해야 하는 게 매우 귀찮았다. 워크트리 개념이 익숙하지 않아서 처음에는 워크트리 생성 시 모든 파일을 다 가져오는 줄 알았다. 워크트리의 동작 방식을 찾아보니, git에 저장된 파일만 생성된다는 걸 새로 알게 되었다. 도움 받은 블로그 글 링크 : Git에서 다수...

Dec 28, 20251 min read

Untitled Publication

37 posts