最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

创建外部模块时,Typescript类型不匹配

运维笔记admin18浏览0评论

创建外部模块时,Typescript类型不匹配

创建外部模块时,Typescript类型不匹配

我正在使用TypeScript并使用nestjs和typeorm创建API。项目中的某些模块通过将它们提取到自己的节点模块中而可以重复使用。这导致外部模块中的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包在节点包中列为peerDependency,因此它应始终相同。

回答如下:

您的外部库似乎有自己的类型定义。在this thread中,建议在node_modules中手动将导入映射到package.json下的文件夹:

"paths": {
  "typeorm": ["node_modules/typeorm"],
  "typeorm/*": ["node_modules/typeorm/*"]
}

这可能只是使用npm link时的问题。

发布评论

评论列表(0)

  1. 暂无评论