본문 바로가기
  • [성공하는 개발자] - Developer
Node/TypeScript

5.배열 Array or Tuple

by Sein-JH 2021. 4. 15.
728x90

Array

  const fruits: string[] = ['사과', '바나나'];
  const scroes: Array<number> = [1, 3, 4];
  function printArray(fruits: readonly string[]) {}
  1. Type[] 방법
  2. Array 방법
  3. readonly Type을 변경하지 못하도록 하기

Tuple -> interface, type alias, class

  let student: [string, number];
  student = ['name', 123];
  student[0]; // name
  student[1]; // 123
  const [name, age] = student;
  1. 서로다른 Type이 있을 때 사용한다.
  2. Tuple 사용을 권장하지 않는다.
  3. 가독성이 너무 않좋고 어떤 데이터가 들어갔는지 확인 방법이 번거롭다
  4. interface, type alias, class 주로 사용한다

'Node > TypeScript' 카테고리의 다른 글

7.Union  (0) 2021.04.17
6.Aliases  (0) 2021.04.15
4.함수 타입(JS →TS)  (0) 2021.04.15
3.기본 타입(2)  (0) 2021.04.14
2. 기본 타입(1)  (0) 2021.04.14

댓글