반응형
Null type
- null이라는 값으로 할당된 것을 null이라고 합니다.
- 있긴 하지만, 사용할 준비가 아직 되지 않은 상태
- null이라는 타입은 null이라는 값만 가질 수 있습니다.(union으로 다른 타입도 함께 받을 수는 있음)
- 런타임에서 typeof를 하게 되면, object로 나옵니다.
let n: null = null;
console.log(n); // null
console.log(typeof n); // object
Undefined type
- 값을 할당하지 않은 변수
- 사용할 준비가 되지 않은 상태
- object의 property가 없을 때도 undefined
- 런타임에서 typeof를 하게 되면, undefined로 나옵니다.
let u:undefined = undefined;
console.log(u); // undefined
console.log(typeof u); // undefined
Union type
- null과 undefined가 아닌 다른 타입에 null 혹은 undefined를 할당하면 오류가 뜨게 됩니다.
- 하지만 이를 가능하게 하기 위해서 union type을 사용할 수 있습니다.
let Name: string = null; // 에러
let u:undefined = null; // 에러
let union:string | null = null;
union = "Eric" // 정상 작동
반응형
'TypeScript' 카테고리의 다른 글
[TS] readonly 프로퍼티 (0) | 2022.07.12 |
---|---|
[TS] Getter & Setter (0) | 2022.07.07 |
[TS] 접근제어자(public, private) (0) | 2022.07.04 |
[TS] compileOptions (outDir,rootDir) (0) | 2022.07.04 |
[TS] Typescript TS6053: file '.ts' not found error (0) | 2022.06.29 |