【问题标题】:typescript custom .d.ts import module does not work打字稿自定义 .d.ts 导入模块不起作用
【发布时间】:2017-05-07 10:55:35
【问题描述】:

我有以下 .d.ts 文件

interface MyInterface {
    login();
    logout();
}

declare module "myOtherModule" {
    export = MyInterface
}

现在在我的 .ts 文件中包含以下内容

import { MyInterface } from '../modules/myOtherModule';

在这里我收到错误“myOtherModule.d.ts”不是模块。这不应该工作吗?如果不是,我该如何解决?

【问题讨论】:

    标签: typescript module


    【解决方案1】:

    要修复它,请尝试在您的 myOtherModule.d.ts 文件中使用:

    export interface MyInterface {
      login();
      logout();
    }
    

    还有你的 foo.ts 文件:

    import { MyInterface } from '../modules/myOtherModule';
    

    .d.ts 文件的编写请参考官方 TypeScript 模板。

    您可以在此处找到一些示例: https://www.typescriptlang.org/docs/handbook/declaration-files/templates.html

    如果您想为自定义模块或类模块编写 .d.ts 文件,这些模板中有很多有用的信息。

    此外,这里还有很多有用的信息: https://www.typescriptlang.org/docs/handbook/declaration-files/deep-dive.html

    【讨论】:

    • 您好,谢谢,但我无法更改 .d.ts 文件,因为这是第三方文件。
    猜你喜欢
    • 2016-07-13
    • 2018-03-04
    • 2019-08-29
    • 2021-02-03
    • 2023-02-09
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2012-11-15
    相关资源
    最近更新 更多