First Type Annotation

honeyricecake·2022년 8월 8일
0

타입스크립트

목록 보기
2/6
let a = "hello"
a = 39

이는 불가능하다.
처음에 hello 가 a에 들어가면서 a의 타입이 string으로 확정되기 때문이다.

let a: string;
a = "Mark"

이렇게 a의 타입을 미리 선언해줄 수 있다.
이러면 a에는 string 외의 다른 타입은 들어갈 수 없다.

let a: number
a = "Mark"

이렇게 하면 오류가 나게 된다.

ex.

function hello(b: number) {
}

hello(39)

이런 식으로도 활용할 수 있다.

0개의 댓글