An example setup:
type A = {
type: 'A';
foo: string;
baz: number;
}
type B = {
type: 'B';
bar: string;
baz: number;
}
type Unioned = A | B
This works fine:
function handle(x:Unioned) {
if (x.type === 'A') {
console.log(x.foo)
}
}
However, I get an error with this:
function handle(x:Omit<Unioned, 'baz'>) {
if (x.type === 'A') {
console.log(x.foo)
}
}
Specifically, the error I get is: Property 'foo' does not exist on type 'Omit<Unioned, "baz">'
.
I would expect that typescript can infer the type of x
within the if statement is actually Omit<A, 'baz'>
, but it doesn't.
What is it about using Omit
that causes this to happen? And how can I make this work as expected?