[S6] 배열 타입

https://github.com/OneMoreBottlee/TypeScript-Master/tree/main/S6

배열 대괄호 앞에 어떤 타입인지 밝혀야함

const activeUsers: string[] = [];
activeUsers.push("steven");

const ageList: number[] = [];
ageList.push(135);

또 다른 배열 타입 선언 방법

// 또다른 선언 방법
const boos: Array<boolean> = [];
// const booa: boolean[] = []

커스텀 타입으로 배열 타입을 선언할 수 있음

// 커스텀으로도 선언할 수 있음
type Point = {
  x: number;
  y: number;
};

const coords: Point[] = [];
coords.push({ x: 23, y: 8 });

다차원 배열

다차원 배열의 타입도 선언할 수 있다.

// 다차원 배열
const board: string[][] = [
    ["X", "O", "X"],
    ["X", "O", "X"],
    ["X", "O", "X"]
]

const demo: number [][][] = [[[1]]]

대괄호를 중복하는게 포인트

Last updated

Was this helpful?