본문 바로가기
Language/Typescript

타입챌린지 : 533-Concat (easy)

by hsloth 2023. 3. 4.

 

이 글은 제가 타입챌린지를 하면서 해석한 내용을 적는 글입니다. 틀린 내용이 있으면 댓글 달아주시면 감사하겠습니다.

 

https://github.com/type-challenges/type-challenges/blob/main/questions/00533-easy-concat/README.md

 

GitHub - type-challenges/type-challenges: Collection of TypeScript type challenges with online judge

Collection of TypeScript type challenges with online judge - GitHub - type-challenges/type-challenges: Collection of TypeScript type challenges with online judge

github.com

 

뭐지? 이제 easy 정도는 쉽다는 건가... 왤케 쉬울까요...?

 

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

// test case
type cases = [
  Expect<Equal<Concat<[], []>, []>>,
  Expect<Equal<Concat<[], [1]>, [1]>>,
  Expect<Equal<Concat<[1, 2], [3, 4]>, [1, 2, 3, 4]>>,
  Expect<Equal<Concat<['1', 2, '3'], [false, boolean, '4']>, ['1', 2, '3', false, boolean, '4']>>,
]

type Concat : Concat이라는 타입을 정의한다.

<T extends Array<any>, U extends Array<any>> : 뒤에서 전개 연산자(...)를 사용해야하므로 Array 타입을 상속받아야 한다. any[]를 상속받아도 상관없다.

[...T, ...U] : 배열 T와 배열 U를 전개연산자를 사용해서 전개한 후, 배열에 담는다.

ex) const arr = [1,2,3,4] 일 때, ...arr은 1, 2, 3, 4 이다.

 

'Language > Typescript' 카테고리의 다른 글

타입챌린지 : Equal - 번외  (0) 2023.03.06
타입챌린지 : 898-Includes (easy)  (0) 2023.03.05
타입챌린지 : 268-If (easy)  (0) 2023.03.04
타입챌린지 : 189-Awaited (easy)  (0) 2023.03.03
타입챌린지 : 43-Exclude (easy)  (0) 2023.03.02