리터럴 타입


let과 const의 차이


let str1 = '1234';   // 타입 : string
const str2 = '1234'; // 타입 : '1234'

as const


[1] 기본 : as const를 사용하지 않는 경우

const minsu = {
    name : 'Minsu',
    age : 22,
};

// { name : string, age: number }
type typeofMinsu = typeof minsu;

// 'name' | 'age'
type keyofTypeofMinsu = keyof typeofMinsu;

// 'string' | 'number'
type MinsuInfo = typeofMinsu[keyofTypeofMinsu];

[2] as const 사용

const minsu = {
    name : 'Minsu',
    age : 22,
} as const;

// { readonly name : string, readonly age: number }
type typeofMinsu = typeof minsu;

// 'name' | 'age'
type keyofTypeofMinsu = keyof typeofMinsu;

// 'Minsu' | 22
type MinsuInfo = typeofMinsu[keyofTypeofMinsu];