We are looking for a safe way to use Object.assign. However, we cannot make it work.
To show our problem, I will use the copyFields method from the Generics documentation
function copyFields<T extends U, U>(target: T, source: U): T { for (let id in source) { target[id] = source[id]; } return target; } function makesrc(): Source { return {b: 1, c: "a"}} interface Source { a?: "a"|"b", b: number, c: "a" | "b" }
I want the engine to not let me create undeclared properties
copyFields(makesrc(), {d: "d"});
We tried to solve this problem by explicitly exposing the types to the copyfields call, but we cannot find a call that will make all the examples work.
For example: To do 5 jobs, you can call copyFields as follows:
copyFields<Source,{a?:"a"|"b"}>(makesrc(), {a: "b"});
but subsequent changes to the source type (for example, removing the option "b") now no longer lead to an error like
Does anyone know how to do this?
source share