【问题标题】:Exporting third party typings with node module使用节点模块导出第三方类型
【发布时间】:2020-03-14 06:38:23
【问题描述】:

我有一个名为module_core 的模块,其目录结构如下:

/src
   /company
      /webservices
         /service-one
         ...
         /service-two
         ...
         /service-new // my service
           index.ts
           constants.ts
           ServiceClient.ts
/Typings

/ServiceClient.ts

export class ServiceClient {
   public constructor() {}...
   public async methodOne() {}...
}
/Typings/@third/party-lib/index.d.ts

...
export class Client {
    constructor(config: object);
    public getUser(userId: string): Promise<User>;
    ...
}
...

我已将第三方输入放入/Typings。我想导出 ServiceClient.ts 和相应的类型以供其他服务使用,例如

index.ts

export * from "../service-new/ServiceClient";
export * from "../../../../Typings/@third/party-lib/index";

但是,这不起作用,或者至少我在其他服务中使用该模块时遇到错误。有关如何使用此文件夹结构正确执行此操作的任何建议?

我确实对tsconfig.json 进行了以下更改,以确保Typings 文件夹是构建过程的一部分:

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

正在使用该模块的服务正在吐出以下错误:

error TS2307: Cannot find module '@third/party-lib'.

1 import { User } from "@third/party-lib";
          ~~~~~~~~~~~~~~~~~

我相信这个错误是由于没有找到类型。

【问题讨论】:

    标签: node.js typescript npm


    【解决方案1】:

    原来问题是我的index.ts 试图导出/Typings 文件夹,但它不应该这样做。我已经将这些复制到使用模块的服务中,因此不需要像我一样导出它们:

    index.ts
    
    export * from "../service-new/ServiceClient";
    export * from "../../../../Typings/@third/party-lib/index";
    

    一旦我将其更改为以下内容,它就可以正常工作了:

    index.ts
    
    export * from "../service-new/ServiceClient";
    

    【讨论】:

      【解决方案2】:

      您是否尝试过像这样删除“../”之一 -

      index.ts
      
      export * from "../service-new/ServiceClient";
      export * from "../../../Typings/@third/party-lib/index";
      

      【讨论】:

      • 谢谢,是的,我不知道是不是文件的位置有问题。尽管您指出我在上面的文件夹结构中错过了一个文件夹是对的,但我已经添加了它。我认为这与正在编译到分发文件夹中的内容有关。我可能在某处遗漏了某个设置,但不确定。
      • 您是否尝试将/typings 目录添加到/src 目录中?
      • 我认为这就是为什么要添加 tsconfig 设置的原因,所以构建过程知道在哪里可以找到它们
      • 部分问题是我忘记将/Typings 复制到使用此模块的服务中。我计划发布类型,所以这一直在测试,虽然并不理想。我认为我仍然有问题,但不是我最初认为的打字问题。
      猜你喜欢
      • 2021-08-25
      • 2012-09-01
      • 1970-01-01
      • 2014-11-04
      • 2020-06-21
      • 1970-01-01
      • 2018-07-22
      • 2021-08-15
      • 1970-01-01
      相关资源
      最近更新 更多