Why does this code not compile?
type A = { n: number }
type B = { s: string }
type Thing = {
a: A
b: B
}
function update(obj: Thing, path: keyof Thing) {
obj[path] = obj[path]
}
I would expect both sides of the assignment to have type A | B but the TypeScript compiler fails with:
error TS2322: Type 'A | B' is not assignable to type 'A & B'.
Type 'A' is not assignable to type 'A & B'.
Property 's' is missing in type 'A' but required in type 'B'.
10 obj[path] = obj[path]
~~~~~~~~~
Is there a way to make it work?