"Using Type Parameters in Generic Constraints" in the TypeScript site shows the example code below. But the following error occurred:
'Type 'U[keyof U]' is not assignable to type 'T[keyof U]'. Type 'U' is not assignable to type 'T'.'
function copyFields<T extends U, U>(target: T, source: U): T {
for (let id in source) {
target[id] = source[id];
}
return target;
}
let x = { a: 1, b: 2, c: 3, d: 4 };
copyFields(x, { b: 10, d: 20 });
In fact, this does not run in the Playground. What is wrong with the code?