본문 바로가기
Q&A해결2025년 2월 3일1분 읽기

TypeScript에서 as const 쓰면 타입이 너무 좁아져서 함수 인자로 못 넘깁니다

데이터사이언스현
조회 654댓글 2

��체에 as const를 붙이면 리터럴 타입이 되어서 함수 인자로 넘길 때 타입 에러가 발생합니다.

const config = { mode: 'production' } as const
function init(opts: { mode: string }) { ... }
init(config) // Error: 'production' is not assignable to string

댓글 0

아직 댓글이 없습니다.
Ctrl+Enter로 등록