발자취/Typescript
[Typescript] 노마드코더 TypeScript로 블록체인 만들기 (2-1)
보그
2022. 8. 9. 19:56
노마드 코더 강의, Typescript로 블록체인 만들기 강의를 듣기로 했다.
암시적 유형 VS 명시적 유형(Impliecit Types VS Explicit Types)
let a = "hello"
1. 타입 추론 string이라고 ts가 추론한다.
let b : boolean = false
let b = false
2. Type Checker 와 소통하는 방식, 형식을 지정
b가 boolean값이라고 명시하고 있다.
let c = [1, 2, 3]
number[array]라고 추론해주고 있다.
c.push("1")
number[array]이기 때문에 string "1" push할 수 없다.
만약 배열안에 아무런 값도 없다면 아래와 같이 Type을 지정해줘야 한다.
let c : number[] = [] , 명시적 선언(최소한으로 사용하는게 좋다.)
let d = []
// any, never array
const player = {
name : "nico"
}
player.name = 1 , type error Name is string
player.hello() , hello does not exist
hello라는 값이 player안에 존재하지 않음, Name은 String이기 때문에 Number인 1은 허용되지 않는다.