"매 fresh object literal 의 only 의 strict property check". TypeScript 매 structural typing 매 normally 의 extra properties 의 allow, 매 but 의 fresh object literal 의 directly 의 typed slot 의 assign 시 매 extra properties 의 error 의 raise. Typo 방지 + intent clarity 의 design choice.
매 핵심
매 Why exists
매 structural typing 매 {a:1, b:2} 의 {a:number} 의 assignable.
매 그러나 매 literal 매 typo 의 high risk — { colour: 'red' } 매 { color?: string } 매 silent ignore.
매 TS 매 fresh literal 의 special-case — 매 Object literal may only specify known properties 의 error.
매 Fresh-ness 매 lost
매 variable 의 assign 시 매 widened — const x = { extra: 1 }; fn(x) 매 ok.
매 spread 매 fresh-ness 의 keep (TS 4.0+).
매 type assertion as T 매 bypass.
매 index signature [k: string]: ... 매 disable.
매 응용
React props typo detection.
Config object validation.
API request body shape enforcement.
Discriminated union narrowing aid.
💻 패턴
Basic excess error
interfacePoint{x: number;y: number}constp: Point={x: 1,y: 2,z: 3};// ^^^^ Object literal may only specify known properties
Bypass via intermediate variable
consttmp={x: 1,y: 2,z: 3};constp: Point=tmp;// OK — fresh-ness lost
// Use this only when extra properties are intentional
Index signature 의 opt-out
interfaceConfig{name: string;[key: string]:unknown;// 매 extra props 의 allow
}constc: Config={name:'x',debug: true,port: 3000};// OK
React props 의 typo guard
typeButtonProps={label: string;onClick:()=>void};functionButton(props: ButtonProps){/* ... */}<Buttonlabel="Save"onCick={save}/>// ^^^^^ Property 'onCick' does not exist
// Excess property check catches typo at JSX call site
Discriminated union with strict checks
typeShape=|{kind:'circle';radius: number}|{kind:'square';side: number};consts: Shape={kind:'circle',radius: 5,side: 3};// ^^^^ excess
// 매 kind 의 narrow 의 'circle', 매 side 의 not allowed
Spread 의 preserve fresh-ness (TS 4.0+)
typeT={a: number};constbase={a: 1};constx: T={...base,b: 2};// 매 error — b 의 excess
Optional excess via Exact type emulation
typeExact<T,UextendsT>=T&{[KinExclude<keyofU,keyofT>]:never;};functionstrict<T>(){return<UextendsT>(x: Exact<T,U>):T=>xasT;}constpoint=strict<Point>()({x: 1,y: 2,z: 3});// ^^^^ Type 'number' is not assignable to 'never'
// 매 variable assign path 의 also 의 strict
Util: 매 satisfies operator (TS 4.9+)
constconfig={endpoint:'https://api.example.com',timeout: 5000,retries: 3,}satisfies{endpoint: string;timeout: number};// ^^^^ 매 retries 의 excess error
매 결정 기준
상황
Approach
Config / DTO literal
매 default check 의 leverage
Plugin system 의 unknown extras
매 index signature 의 add
Test fixture 의 extra debug fields
매 intermediate var 또는 as
매 strict 의 want 의 variable path
매 Exact helper 또는 satisfies
Library author 의 strict API
매 satisfies 또는 nominal brand
기본값: 매 default behavior 의 leverage — 매 typo 매 catch. 매 escape 매 minimize, 매 satisfies 의 prefer.
언제: 매 typo-prone literal — config, props, API body. Schema-bound inputs.
언제 X: 매 plugin / metadata 의 extra props 매 intentional — 매 index signature 의 add.
❌ 안티패턴
as T 의 bypass habit: 매 error 의 mask, 매 typo 의 ship — 매 fix 의 root cause.
Index signature 의 over-permissive: 매 모든 class 매 [k: string]: any 의 add — 매 type safety 의 destroy.
Intermediate var 의 escape: 매 const x = {...}; fn(x) 매 intent 의 obscure — 매 명시적 cast 의 prefer.
satisfies 의 ignore: 매 TS 4.9+ 매 widening 없는 strict literal 의 best tool — 매 underused.