【问题标题】:Reexport all typings inside a typescript .d.ts definition file重新导出打字稿 .d.ts 定义文件中的所有类型
【发布时间】:2020-10-14 14:15:07
【问题描述】:

有没有办法在另一个.d.ts file 中重用来自一个.d.ts 文件的类型来声明一个模块?

例如,假设我们有以下供应商文件:

node_modules/some-fancy-module/types.d.ts

  export const valueA: number;
  export const valueB: number;

现在在我的项目文件中,我想声明所有*.special-file 导入将返回valueAvalueB,如https://www.typescriptlang.org/docs/handbook/modules.html 所示。

typings.d.ts

declare module '*.special-file' {
  export * from 'some-fancy-module/types.d.ts';
}

很遗憾,我无法使用这些类型:

import {valueA} from './my.special-file';

错误Module '"*.special-file"' has no exported member 'valueA'

截图:

如屏幕截图所示的 CodeSandbox: https://codesandbox.io/s/reexport-types-5h8hb?file=/src/index.ts

【问题讨论】:

  • 其实你的代码运行的很好,能分享一下你的tsconfig吗?
  • 我添加了一个带有 tsconfig 和问题的代码框

标签: typescript typescript-typings


【解决方案1】:

您可以在声明中导入然后导出。以下作品:

declare module '*.foo' {
  import {valueA} from './third-party/types.d.ts';
  export = valueA
}

【讨论】:

    【解决方案2】:

    Wildcard module declarations 中明确指出,在使用通配符声明模块后,您还需要导入它,但在您发布的代码中您不需要。

    您也可以尝试在通配符模块声明之后导入模块。

    希望这会有所帮助。

    【讨论】:

    • 我不明白你的意思 - 我的最后一个代码 sn -p 显示了导入语句。
    • 对不起@jantimon ,如果import {valueA} from './my.special-file';declare module '*.special-file' { 在同一个文件中,则表示我不明白,我将简单地删除这个无用的答案
    【解决方案3】:

    这是您的沙盒分支 #1:https://codesandbox.io/s/reexport-types-ovrtt?file=/tsconfig.json

    它也适用于外部库https://github.com/Drag13/reexport/blob/master/src/index.ts

    如果你不想使用通配符import test from "*.special-file",你仍然可以使用命名导入:

    import test from "test.special-file";

    但是,它有一个大问题。代码编译完美,但无法正常工作。编译后的 index.js 看起来像:

    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var test_special_file_1 = require("test.special-file");
    console.log(test_special_file_1.hello() + " " + test_special_file_1.test());
    

    当然,test.special-file 不存在。

    【讨论】:

    • 但我不想导入该目录中的所有文件 - 所以我不能像你在这里那样使用星号:import test from "*.special-file"
    • 好的,改成import test from "test.special-file";。它也有效,@jantimon
    • 酷 - 你能解释一下它是如何工作的吗?我也可以重新导出它吗?在不更改第三方代码的情况下也将其用于.special-file2
    • 它只是声明和通配符的组合。实际上与declare module "*.json" 的工作方式相同。但我不明白第二个问题,你到底想再导出什么?
    • 我无法将第三方代码更改为declare module,这就是为什么我想使用我自己的类型文件中的类型,如屏幕截图所示
    【解决方案4】:

    我不知道你的 some-fancy-module 包到底是什么,但是当我尝试使用“我的”包时它仍然可以正常工作。

    而且你的代码框不正确,因为在环境模块中相对模块是不可接受的。

    这是我的例子:我使用n8n-nodes-chatwork 作为some-fancy-module

    typings.d.ts

    declare module '*.foo' {
      export * from 'n8n-nodes-chatwork/dist/credentials/Chatwork.credentials.d'; // removed ".ts"
    }
    
    

    index.ts,Chatwork 作为你的valueA

    import {Chatwork} from './demo.foo';
    
    type x = typeof Chatwork;
    

    import test from './demo.foo';
    
    type x = typeof test.Chatwork;
    

    这是我的 sanbox:https://codesandbox.io/s/reexport-types-05ftm?file=/src/index.ts

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 2020-09-08
      • 2018-03-04
      • 2018-11-06
      • 1970-01-01
      • 2020-03-18
      • 2016-09-02
      • 2018-01-19
      • 2018-08-30
      相关资源
      最近更新 更多