首页 > 解决方案 > TypeScript 编译器是否存在关于泛型的错误,或者我遗漏了什么?

问题描述

我遇到了一个问题,这似乎很难解决。假设我有以下代码:

interface HasId {
    id: string;
}

type Combiner<T> = {
    [key in keyof T]: T[key];
}

function testfn<T extends HasId>(args: Combiner<T>) {

}

class Person<T extends HasId> implements HasId {
    id: string;
    test() {
        testfn<T>({ id: this.id });
    }
}

此代码只是示例,但不应有任何错误。但是,问题出现在这条线附近:

testfn<T>({ id: this.id })

它总是,总是,总是在编译时抛出这个错误:

Argument of type '{ id: string; }' is not assignable to parameter of type 'Combiner<T>'

但问题是,错误是错误的。{ id: string }符合该Combiner类型的规格!

不仅如此,在 VSCode 上,它还建议id我在调用时添加该属性testfn,这让我相信这是一个编译器错误。

我错了,还是其他人也在处理这个错误?

编辑:

这是我的tsconfig.json

{
    "compilerOptions": {
        "strict": true,
        "experimentalDecorators": true,
        "module": "CommonJS",
        "target": "ES2019",
        "lib": ["ES2019"],
        "rootDir": "./src",
        "outDir": "./dist",
        "esModuleInterop": true,
        "strictPropertyInitialization": false
    },
    "include": [
        "./src"
    ],
    "exclude": [
        "./node_modules",
        "./dist"
    ]
}

标签: javascripttypescriptcompiler-errors

解决方案


问题在于以下表达式:

testfn<T>({ id: this.id });

不能保证字面值{ id: this.id }extends T。它确实 extend HasId,但这还不够。

您可能不是在处理 a Person<HasId>,而是Person<{ id: 1, name: 'John' }>在某个时候处理 a ,这就是发生错误的原因:

{ id: this.id }不可分配给{ id: number; name: string }

如果你只关心id里面的值testFn,它不应该需要参数化类型T,但它可能足以编写(这解决了问题):

function testfn(args: Combiner<HasId>) {

}

但我不确定你打算Combiner对以前依赖的类型做什么T


推荐阅读