我正在使用 TypeScript 并使用 Nestjs 和 typeorm 创建 API。项目中的一些模块通过将其提取到自己的 Node 模块中来实现可重用。这导致外部模块中的 Repository
类型与原始包中的 Repository
类型不同。因此,当使用外部模块时,我收到错误
Argument of type 'import("/Users/user/project/node_modules/typeorm/repository/Repository")
.Repository<import("/Users/project/src/user/user.entity").User>'
is not assignable to parameter of type
'import("/Users/user/my-module/node_modules/typeorm/repository/Repository")
.Repository<import("/Users/project/src/user/user.entity").User>'.
typeorm 包在 Node 包中被列为对等依赖项,因此它应该始终相同。
最佳答案
您的外部库似乎有自己的类型定义。在 this thread ,建议在 package.json
中手动将导入映射到 node_modules
下的文件夹:
"paths": {
"typeorm": ["node_modules/typeorm"],
"typeorm/*": ["node_modules/typeorm/*"]
}
这可能仅在使用 npm link
时出现问题。
关于node.js - 创建外部模块时 typescript 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54747912/