Concept Flow - Omit type
Start with full type
Choose keys to omit
Remove chosen keys from type
Resulting type without omitted keys
Omit type creates a new type by removing specified keys from an existing type.
type Person = { name: string; age: number; city: string };
type PersonWithoutAge = Omit<Person, 'age'>;
const p: PersonWithoutAge = { name: 'Alice', city: 'Paris' };| Step | Action | Type Before | Keys Omitted | Type After |
|---|---|---|---|---|
| 1 | Define Person type | N/A | N/A | { name: string; age: number; city: string } |
| 2 | Apply Omit<Person, 'age'> | { name: string; age: number; city: string } | 'age' | { name: string; city: string } |
| 3 | Declare variable p | { name: string; city: string } | N/A | p = { name: 'Alice', city: 'Paris' } |
| 4 | Try to add omitted key 'age' to p | p type excludes 'age' | 'age' | Error: Property 'age' does not exist on type 'PersonWithoutAge' |
| Variable | Start | After Omit | Final |
|---|---|---|---|
| p | undefined | { name: string; city: string } | { name: 'Alice', city: 'Paris' } |
Omit<Type, Keys> creates a new type by removing Keys from Type. Syntax: type NewType = Omit<OldType, 'key1' | 'key2'>; Use it to exclude properties you don't want. Original type stays unchanged. Trying to use omitted keys causes errors.