TypeScript Type Annotation2 (변수 타입 선언)
string
이나 number
말고도 array
나 object
에도 규칙이나 타입을 선언 해줄수도 있다.
// 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 |
댓글
이 글 공유하기
다른 글
-
Function (함수)
Function (함수)
2021.05.01 -
Interface (인터페이스)
Interface (인터페이스)
2021.04.30 -
TypeScript Type Annotation (변수 타입 선언)
TypeScript Type Annotation (변수 타입 선언)
2021.04.28 -
TypeScript 설치
TypeScript 설치
2021.04.27