본문 바로가기
Q&A2025년 2월 14일1분 읽기

TypeScript satisfies 연산자 활용

양
양수진
조회 465댓글 2

TypeScript의 satisfies 연산자가 뭐고 언제 쓰나요?

댓글 2

김김영삼1년 전

satisfies는 타입을 검증하면서 추론된 타입을 유지하는 연산자입니다:

type Config = Record<string, string | number>

// as 사용 — 타입 정보 손실
const config1 = { port: 3000, host: "localhost" } as Config
config1.port.toFixed() // ❌ string | number라서 에러

// satisfies 사용 — 타입 검증 + 추론 유지
const config2 = { port: 3000, host: "localhost" } satisfies Config
config2.port.toFixed() // ✅ number로 추론됨!
config2.host.toUpperCase() // ✅ string으로 추론됨!

설정 객체, 라우트 정의 등에서 타입 안전성과 추론을 동시에 얻을 때 유용합니다.

나나윤아(guest)1년 전
초보에게 정말 유용한 글이네요. 북마크합니다.
Ctrl+Enter로 등록