我遇到了一个问题,似乎很难解决。假设我有以下代码:
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>) {
}
但是我不确定您打算对以前取决于Combiner
的T
类型做什么。
关于javascript - TypeScript编译器是否存在有关泛型的错误,还是我缺少某些东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62803872/