객체에 as const를 붙이면 리터럴 타입이 되어서 함수 인자로 넘길 때 타입 에러가 발생합니다.
const config = { mode: 'production' } as const
function init(opts: { mode: string }) { ... }
init(config) // Error: 'production' is not assignable to string객체에 as const를 붙이면 리터럴 타입이 되어서 함수 인자로 넘길 때 타입 에러가 발생합니다.
const config = { mode: 'production' } as const
function init(opts: { mode: string }) { ... }
init(config) // Error: 'production' is not assignable to string
댓글 2
as const로 선언하면
'production'리터럴 타입이 됩니다. 해결 방법:satisfies가 가장 권장됩니다. 타입 검증하면서 추론도 유지합니다.