It makes sense that it is Unot assigned T, since an object that satisfies Ucan have additional fields that it Tdoes not have:
interface Foo { foo: number; }
interface Bar extends Foo { bar: number; }
interface Bar2 extends Foo { bar: string; }
function assign<T extends U, U>(b: U): T {
const returnVal: T = b;
return returnVal;
}
const bar2: Bar2 = { foo: 7, bar: "happy" };
assign<Bar, Foo>(bar2);
, U T, , U[keyof U] T[keyof U].
( 100% , , , .)
, , , copyFields, , :
function copyFields<T, K extends keyof T>(target: T, source: Pick<T, K>) {
for (let id in source) {
target[id] = source[id];
}
return target;
}