【问题标题】:Typescript type missmatch when creating external module创建外部模块时打字稿类型不匹配
【发布时间】:2019-02-18 22:34:59
【问题描述】:

我正在使用 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 包在 node 包中被列为 peerDependency,因此它应该始终相同。

【问题讨论】:

    标签: node.js typescript nestjs typeorm


    【解决方案1】:

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

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

    这可能只是在使用 npm link 时才会出现的问题。

    【讨论】:

    • Ops,这实际上是我的错字... Repository 是从不同的路径导入的,但不是从 User 类型导入的。
    • 嗯,好的。 :) 你是从 npm 安装包还是使用 npm 链接?
    • 我使用了 npm 链接
    猜你喜欢
    • 2014-09-28
    • 2016-06-22
    • 2022-01-18
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多