javascript - TypeScript编译器是否存在有关泛型的错误,还是我缺少某些东西?

标签 javascript typescript compiler-errors

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

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"
    ]
}

最佳答案

问题是在以下表达式中:

testfn<T>({ id: this.id });
不保证字面值{ id: this.id }扩展T。它确实扩展了HasId,但这还不够。
您可能没有在处理Person<HasId>,但是例如在某个时候处理了Person<{ id: 1, name: 'John' }>,这就是发生错误的原因:{ id: this.id }无法分配给{ id: number; name: string }如果仅在id中关心testFn值,则它不需要参数化类型T,但是编写它就足够了(解决了问题):
function testfn(args: Combiner<HasId>) {

}
但是我不确定您打算对以前取决于CombinerT类型做什么。

关于javascript - TypeScript编译器是否存在有关泛型的错误,还是我缺少某些东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62803872/

相关文章:

javascript - 找到最近的包含 .myClass 的前一个兄弟

javascript - 无法读取未定义的属性 '[function]'

typescript - 无法将字符串值分配给 typescript 枚举(初始化程序类型字符串不可分配给变量类型)

reactjs - 使用 react.js 和 promise 解析错误 : Unexpected token, 预期为 ","

android - 无法弄清楚 "cannot be provided without an @provides-annotated method"错误

javascript - 如何使用 AngularJS 使用按钮将 +1 添加到字段?

javascript - 将对象数组添加到一起

javascript - System.js 不显示完整的堆栈跟踪

c++ - 类名未声明类型 C++

javascript - 如何在 Photoshop 中使用 JavaScript 脚本翻转图层的选定区域?