javascript - 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"
]
}
解决方案
问题在于以下表达式:
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
。
推荐阅读
- html - 导航栏下拉问题
- html - 字体不显示?
- python - 如何对dump语句中的json进行排序?
- python - 滚动查看在 Python 中使用 pyAutoGUI 填写 Web 表单错误
- plugins - 记分牌在我的世界插件中不可见
- apache-kafka - 错误在对 /favicon.ico 的 REST 调用中未捕获的异常 (org.apache.kafka.connect.runtime.rest.errors.ConnectExceptionMapper:61)
- java - 足迹肥皂 api + maven-jaxb2-plugin
- reactjs - 登录后反应路由器未正确路由
- php - 在 PHP 中为 JSON 创建 DateTime
- powerbi - PowerBi - 更新 PowerBi Cloud 上的报告