-
Notifications
You must be signed in to change notification settings - Fork 0
Breaking Programming TypeScript
kskim7714 edited this page Apr 23, 2021
·
3 revisions
- 타입 추론 개념을 이해하려면 꼭 숙지.
- 타입을 (정밀이 아닌) 일반적으로 추론 함.
- let/var은
리터럴 값이리터럴이 속한 기본 타입으로 넓혀짐.
let a = 'x' // string
const d = { x: 3 } // {x: numbr}
enum E {X, Y, Z}
let e = E.X // E- const로 할때 타입이 어떻게 될까?
const a = 'x' // 'x'
enum E {X, Y, Z}
const e = E.X // E.X- 타입을 명시하면 타입이 넓어지지 않음.
let a: 'x' = 'x' // 'x'
const d: {x: 3} = {x: 3} // {x: 3}- 타입 선언 없이 할당, const에 할당 후에 let/var로 재할당하면 타입이 넓어짐.
const a = 'x' // 'x'
let b = a // string- 타입 선언 포함 할당, const에 할당 후에도 let/var로 해도 타입이 넓어지지 않음.
const c:'x' = 'x' // 'x'
let d = c // 'x'- null/undefined로 초기화하면 any 타입으로 넓어짐.
let a = null // any
a = 3 // any
a = 'b' // any
- null/undefined로 초기화된 변수가 선언 범위를 벗어나면 좁은 타입으로 변환됨.
function x() {
let a = null // any
a = 3 // any
a = 'b' // any
return a
}
// client calls
x() // string