Pick and Omit
Concept and syntax
Pick<T,K> projects selected keys; Omit<T,K> projects all keys except the specified ones. Both are static shapes and do not redact an existing object at runtime. Pick restricts K to keys of T, whereas Omit can name keys that are absent.
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 Row={id:number;label:string;secret:string}; type Public=Omit<Row,"secret">; type ID=Pick<Row,"id">;const p:Public={id:1,label:"tea"}; const i:ID={id:p.id}; console.log(i.id,p.label);Expected output:
1 teaCommon mistake
Intentionally invalid; compile separately and do not execute.
type Row={id:number;label:string;secret:string}; type Public=Omit<Row,"secret">; type ID=Pick<Row,"id">;const p:Public={id:1,label:"tea",secret:"x"};Expected diagnostic: TS2353. The fresh projected literal contains an excluded property.
Practice and recap
Assign a wider variable to Public and inspect Object.keys.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Pick and Omit — knowledge check
Check the type transformation, trace output, diagnose a rejected example, and apply the concept to a new case.
Which statement about Pick and Omit is accurate?
What is the complete stdout, represented as a JSON string (including newline characters)?
type Row={id:number;label:string;secret:string}; type Public=Omit<Row,"secret">; type ID=Pick<Row,"id">;const p:Public={id:1,label:"tea"}; const i:ID={id:p.id}; console.log(i.id,p.label);Which diagnostic and explanation match the rejected example under the lesson settings?
type Row={id:number;label:string;secret:string}; type Public=Omit<Row,"secret">; type ID=Pick<Row,"id">;const p:Public={id:1,label:"tea",secret:"x"};What is the effect of Omit<Row,"missing">?