Exclusive and at-least-one patterns
Concept and syntax
Optional never fields can express exclusive alternatives, while a mapped union of required picks can require at least one property. With exactOptionalPropertyTypes disabled, explicit undefined may satisfy an optional never field. Neither pattern checks untrusted data or guarantees absence of unrelated structural keys.
Working example
Save each snippet separately as main.ts in a fresh directory with TypeScript 5.8.3. Compile with tsc --strict --target ES2022 --module commonjs --moduleDetection force --skipLibCheck --noEmitOnError main.ts and run accepted snippets with node main.js. Type aliases are erased; assignments and diagnostic checks demonstrate the type relations, not the printed output alone.
type One<T>={[K in keyof T]-?:Required<Pick<T,K>>&Partial<Omit<T,K>>}[keyof T]; type Filter=One<{name:string;id:number}>; type Choice={email:string;phone?:never}|{phone:string;email?:never};const f:Filter={id:3}; const c:Choice={email:"a@b"}; console.log(f.id,c.email);Expected output:
3 a@bCommon mistake
Intentionally invalid; compile separately and do not execute.
type One<T>={[K in keyof T]-?:Required<Pick<T,K>>&Partial<Omit<T,K>>}[keyof T]; type Filter=One<{name:string;id:number}>; type Choice={email:string;phone?:never}|{phone:string;email?:never};const f:Filter={};Expected diagnostic: TS2322. At least one selected key must be required and present.
Independent comparison
Both actual contact values violate the exclusive alternatives. Expected: TS2322
type Choice={email:string;phone?:never}|{phone:string;email?:never}; const x:Choice={email:"a@b",phone:"123"};Independent comparison
With the baseline option disabled, explicit undefined is accepted. Expected: 'a@b\n'
type Choice={email:string;phone?:never}|{phone:string;email?:never}; const x:Choice={email:"a@b",phone:undefined}; console.log(x.email);Independent comparison
With --exactOptionalPropertyTypes, explicit undefined is rejected. Expected: TS2375
type Choice={email:string;phone?:never}|{phone:string;email?:never}; const x:Choice={email:"a@b",phone:undefined}; console.log(x.email);Practice and recap
Try both keys in Filter and both keys in Choice; compare results.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Exclusive and at-least-one patterns — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Exclusive and at-least-one patterns is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type One<T>={[K in keyof T]-?:Required<Pick<T,K>>&Partial<Omit<T,K>>}[keyof T]; type Filter=One<{name:string;id:number}>; type Choice={email:string;phone?:never}|{phone:string;email?:never};const f:Filter={id:3}; const c:Choice={email:"a@b"}; console.log(f.id,c.email);Which diagnostic and explanation match the rejected example under the lesson settings?
type One<T>={[K in keyof T]-?:Required<Pick<T,K>>&Partial<Omit<T,K>>}[keyof T]; type Filter=One<{name:string;id:number}>; type Choice={email:string;phone?:never}|{phone:string;email?:never};const f:Filter={};Which shape satisfies Filter but not necessarily exclusivity?