타입 챌린지 21106 - Combination key type

소파의 벨로그·2025년 5월 23일

타입챌린지

목록 보기
92/131

문제 링크

문제

  1. 복수의 수정키를 결합하되, 같은 수정 키 조합은 나오면 안된다.
  2. 제공된 ModifierKeys에서, 이전 값의 우선 순위는 뒤에 있는 값의 우선순위보다 높다;
    cmd ctrl은 괜찮지만, ctrl cmd는 허용되지 않는다.

  1. Combine multiple modifier keys, but the same modifier key combination cannot appear.
  2. In the ModifierKeys provided, the priority of the previous value is higher than the latter value; that is, cmd ctrl is OK, but ctrl cmd is not allowed.

내 풀이

type Combs<T extends string[]> =
  T extends [infer Start extends string,...infer Rest extends string[]]?
    `${Start} ${Rest[number]}`|Combs<Rest>
  :never

여타 조합문제와 비슷하게 재귀로 접근한다.

관련 개념

`${특정문자}${string union}` 타입은 분배된다는 점을 이용한 풀이이다.

예를 들어 `${'A'}${'B'|'C'}`'AB'|'AC'와 같다.

0개의 댓글