Wondering how to make this code work in TypeScript. Function tempA
copies the value of x.a to x.temp and returns it, while function tempB
does the same with x.b. However, when calling tempA
, the compiler seems to forget about the existence of the b
field on x
. Is there a workaround for this issue? Is there a GitHub problem addressing this scenario or which feature category should it be reported under?
let tempA = (x: { a: number; temp?: number}) => {
x.temp = x.a;
return x;
}
let tempB = (x: { b: number; temp?: number}) => {
x.temp = x.b
return x;
}
let x = { a: 4, b: 3 };
let y = tempA(x);
let z = tempB(y); // Error! y.b is considered non-existent by the compiler (even though it exists)