아래와 같이 type annoation을 하게 되면 배열에 값이 추가될 수 없다고 나오게 됩니다.
// type이 빈 배열인 경우
const activeUsers: [] = [];
// Argument of type 'string' is not assignable to parameter of type 'never'
activeUsers.push("soyeon");
빈 배열로만 사용하고 싶은 것이 아니라면 배열 대괄호 앞에 어떤 타입인지를 밝혀줘야 합니다.
const activeUsers: string[] = [];
activeUsers.push("soyeon");
const activeUsers: Array<string> = [];
activeUsers.push("soyeon");
타입을 사용할 수도 있습니다.
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]]];