TypeScript:在通用约束中使用类型参数

时间:2017-01-05 05:16:01

标签: javascript typescript

" Using Type Parameters in Generic Constraints"在TypeScript站点中显示下面的示例代码。但是发生了以下错误:

  

'键入' U [keyof U]'不能分配类型' T [keyof U]'。输入' U'   不能分配给' 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 });

事实上,这不会在Playground中运行。代码有什么问题?

1 个答案:

答案 0 :(得分:3)

有意义U无法分配给T,因为满足U的对象可能包含T不具备的其他字段:

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;  // error: Type 'U' is not assignable to type 'T'.
    return returnVal;
}

const bar2: Bar2 = { foo: 7, bar: "happy" };
assign<Bar, Foo>(bar2);

因此,由于U不能分配给T,我们无法保证特定U[keyof U]可分配给T[keyof U]

(我对这个解释没有100%的信心,但这似乎对我有意义。)

但是,通过修改输入内容的方式,您可以编写copyFields版本,其工作方式符合预期,like this

function copyFields<T, K extends keyof T>(target: T, source: Pick<T, K>) {
    for (let id in source) {
        target[id] = source[id];
    }
    return target;
}
相关问题