Given the following:
type X = {
a: number;
b: number;
};
const a = <T extends X>(t: T): T => {
const { b, ...noB } = t;
const r = { ...noB, b: 1 };
return r;
};
Type of noB
is Omit<T, "b">
.
Type of r
is Omit<T, "b"> & { b: number }
Typescript complains that
Type 'Omit<T, "b"> & { b: number; }' is not assignable to type 'T'.
'Omit<T, "b"> & { b: number; }' is assignable to the constraint of type 'T',
but 'T' could be instantiated with a different subtype of constraint 'X'.ts(2322)
I don’t understand why there is a type mismatch here since it seems clear to me that Omit<T, "b"> & { b: number }
will be always equivalent (assignable) to T
.