Omit a property from each union member
Use a distributive conditional type to omit a shared property and preserve narrowing on the resulting union.
You have a union of shapes that each carry an id, and you want the same union without id for a create form. You wrote Omit<Shape, 'id'> and tsc rejects a circle that has a radius.
type Shape =
| { kind: 'circle'; radius: number; id: string }
| { kind: 'square'; side: number; id: string }
type WithoutId = Omit<Shape, 'id'>
const circle: WithoutId = { kind: 'circle', radius: 2 }
// error TS2353: Object literal may only specify known properties, and 'radius' does not exist in type 'WithoutId'.tsc --noEmit --strict trap.ts prints that error. The circle member of Shape has radius, yet the result of Omit does not.
Omit reads the shared keys
TypeScript defines Omit<T, K> as Pick<T, Exclude<keyof T, K>>. On a union, keyof returns the keys that every member has. I asked for keyof Shape and tsc accepted "id" and "kind" and rejected "radius".
Omit starts from kind and id, removes id, and hands back one object type with kind and nothing else. Both members collapse into it. Narrowing on kind finds no radius either: Property 'radius' does not exist on type 'Omit<Shape, "id">'.
Omit each member
Run Omit on one member at a time and join the results. A conditional type on a bare type parameter does that.
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never
type WithoutId = DistributiveOmit<Shape, 'id'>
const circle: WithoutId = { kind: 'circle', radius: 2 }
const square: WithoutId = { kind: 'square', side: 3 }
function area(shape: WithoutId): number {
if (shape.kind === 'circle') {
return Math.PI * shape.radius ** 2
}
return shape.side ** 2
}
function create(input: WithoutId): Shape {
return { ...input, id: 'a1' }
}
console.log(area(circle).toFixed(2), area(square)) // prints 12.57 9
console.log(create(square)) // prints { kind: 'square', side: 3, id: 'a1' }With the same Shape above it, this compiles under --strict, and npx tsx fixed.ts prints 12.57 9 and the new record. Inside area, the kind check narrows shape to the circle member, so radius is available. create spreads the input, adds the id, and returns a full Shape without a cast.
A circle object literal with an extra side property still fails the excess property check. I tried it, and tsc printed 'side' does not exist in type 'Omit<{ kind: "circle"; radius: number; id: string; }, "id">', naming the one member it compared against.
I reach for this when a stored record is a union and the caller supplies everything except generated fields such as id.