gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:56:23 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com