만두맨두
만두 개발자 되다.
만두맨두
전체 방문자
오늘
어제
  • 분류 전체보기 (27)
    • 면접준비 (2)
    • GitHub (1)
    • JavaScript (5)
    • TypeScript (6)
    • REACT (1)
    • 에러 해결일지 (2)
    • 알고리즘 (7)
      • 이론 (4)
      • 자료구조 (1)
      • 프로그래머스 (2)
    • 만두의 일기 (1)
    • 프로젝트 (2)
      • 팀블로그 (2)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • length
  • node.js
  • 배열의 길이
  • hoisting
  • dp
  • 00018-easy-tuple-length
  • 네임스페이스
  • PriorityQueue
  • JS
  • 다이나믹 프로그래밍
  • 점 표기법
  • TypeScript
  • frontend
  • 대괄호 표기법
  • eslint
  • Beakjoon
  • telling me
  • 호이스팅
  • 타입 챌린지
  • programmers
  • 타입챌린지
  • react
  • JavaScript
  • TellingUs
  • type-challenges
  • Algorithm
  • 프로그래머스
  • 우선순위큐
  • 자바스크립트
  • dynamic programming

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
만두맨두

만두 개발자 되다.

TypeScript

[ TypeScript ] type-challenges-00018-easy-tuple-length

2023. 8. 1. 21:17

00018-easy-tuple-length

[ 문제 설명 ]
For given a tuple, you need create a generic `Length`, pick the length of the tuple

[ 문제 ]
type Length<T> = any​


[ 예시 ]

type tesla = ['tesla', 'model 3', 'model X', 'model Y']
type spaceX = ['FALCON 9', 'FALCON HEAVY', 'DRAGON', 'STARSHIP', 'HUMAN SPACEFLIGHT']

type teslaLength = Length<tesla>  // expected 4
type spaceXLength = Length<spaceX> // expected 5

튜플(readonly 배열) 타입의 길이를 가져올 수 있는 Length 타입을 정의하는 문제입니다.


사실 이번 문제의 설명이라고 하면,,, 어떻게 길이를 가져올 수 있는가에 대한 설명일텐데요...

그 설명을 00014-easy-first에서 다 해버렸습니다.. 🥲

아래 링크에 자세히 설명되어 있으니까 한 번씩 봐주시면 좋을 것 같아요 : )

 

[ TypeScript ] type-challenges - 00014-easy-first

00014-easy-first [ 문제 설명 ] Implement a generic `First` that takes an Array `T` and returns its first element's type. [ 문제 ] type First = any​ [ 예시 ] type arr1 = ['a', 'b', 'c'] type arr2 = [3, 2, 1] type head1 = First // expected to be 'a

gomandoogomandoo.tistory.com


[ 정답 ]
type Length<T extends readonly any[]> = T['length']​

1. T에 튜플이 들어오기 때문에 readonly any[]를 extends 시켜준다.
2. T['length']를 통해 타입의 길이를 구한다.

'TypeScript' 카테고리의 다른 글

[ TypeScript ] type-challenges - 00014-easy-first  (0) 2023.07.24
[ TypeScript ] type-challenges - 00011-easy-tuple-to-object  (0) 2023.07.21
[ TypeScript ] type-challenges - 00007-easy-readonly  (0) 2023.07.08
[ TypeScript ] type-challenges - 00004-easy-pick  (0) 2023.07.06
[TypeScript] 지금까지 만난 타입스크립트 룰 정리본  (0) 2023.04.08
    'TypeScript' 카테고리의 다른 글
    • [ TypeScript ] type-challenges - 00014-easy-first
    • [ TypeScript ] type-challenges - 00011-easy-tuple-to-object
    • [ TypeScript ] type-challenges - 00007-easy-readonly
    • [ TypeScript ] type-challenges - 00004-easy-pick
    만두맨두
    만두맨두
    프론트엔드 개발자가 되고 싶다 !

    티스토리툴바