Here is what i wanna do:
const assign = <T extends U, U>(original: T, changes: U) =>
<T>Object.assign({}, original, changes);
interface IFoo {
bar: number,
baz?: string,
quux?: boolean,
}
let foo = <IFoo>{ bar: 42, baz: 'hello' };
assign(foo, { baz: 'world' }); //Compilation Error:
I just wanna implement a type safe assign. but the compiler throws the error:
Argument of type 'IFoo' is not assignable to parameter of type '{ baz: string; }'. Property 'baz' is optional in type 'IFoo' but required in type '{ baz: string; }'.
The properties of changes type should always be a strict subset of original type, this kind of assignment should be totally logical, I think.
How do I get around it, appeasing the scrupulous compiler without sacrificing the type safety of the changes parameter?