string이나 number말고도 arrayobject에도 규칙이나 타입을 선언 해줄수도 있다.

 

// array type annotation
let nameList: string[];

// error
nameList = [1, 2, 3];
nameList.push(4);

nameList = ["1", "2", "3"];
nameList.push("4");

// array type annotation2
let tuple: [string, number];

// error
tuple = [1, "2"];

tuple = ["1", 2];

// object type annotation
let userList: { name: string, score: number };

// error
userList = {
   age: '123'
}

userList = {
   name: 'sample',
   score: 100
}

let nameList2: string[];

'TypeScript' 카테고리의 다른 글

Enum (열거형 상수)  (0) 2021.05.04
Function (함수)  (0) 2021.05.01
Interface (인터페이스)  (0) 2021.04.30
TypeScript Type Annotation (변수 타입 선언)  (0) 2021.04.28
TypeScript 설치  (0) 2021.04.27