What could be causing the compilation error in this code?
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]
}
It seems like both sides of the assignment should have type A | B
, but unfortunately the TypeScript compiler returns an error:
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]
~~~~~~~~~
Any suggestions on how to resolve this and make it compile successfully?