【问题标题】:Export all interfaces/types from one file in TypeScript project从 TypeScript 项目中的一个文件中导出所有接口/类型
【发布时间】:2017-11-18 21:13:25
【问题描述】:

我的库的直接接口被捕获

index.d.ts

当前自动生成的

index.ts

在我的项目的 package.json 文件中,typings/types 属性指向这个 index.d.ts 文件,据我了解,它应该是这样的。到目前为止一切顺利。

但是,我想从 index.d.ts 中导出更多类型,这些类型当前未在 index.d.ts 中表示。

例如我有一些手动创建的类型

foo.d.ts
bar.d.ts

我希望这些在 index.d.ts 中表达。

我认为可行的唯一方法是将 foo.d.ts/bar.d.ts 类型导入 index.d.ts,然后从该文件中导出它们。

但是,我遇到了麻烦!

这是我尝试过的:

// index.ts

import {IBar} from '../bar.d.ts'
import {IFoo} from '../foo.d.ts'

// ... do some stuff with index.ts yadda yadda

export type IBar = IBar; // doesn't seem to work
export interface IFoo = IFoo; // doesn't seem to work

这可能吗?我该怎么做?

【问题讨论】:

    标签: node.js typescript typescript2.0


    【解决方案1】:

    如果不需要,可以直接导出。

    export * from '../foo/bar';
    

    或者,如果您只需要导出其中的一部分,您可以使用。

    export { IFoo, IBar} from '../foo/bar';
    

    或者,如果您想在导出期间重命名它们,您可以这样做:

    export { IFoo as IOther, IBar as Dogs } from '../foo/bar';
    

    您可以在 MDN 文档here 中阅读有关它们的更多信息。

    【讨论】:

    • 太棒了!正是我需要的
    【解决方案2】:

    我认为其他答案会起作用。如果您确实需要在文件中使用 IFoo 和 IBar,那么可以这样做:

    import * as IFooImport from '../foo.d.ts';
    
    // use IFooImport
    
    export import IFoo = IFooImport.IFoo;
    

    这可行,但使用“导出导入”语法有点尴尬。

    【讨论】:

    • 感谢您的提示!为了导出所有内容,我做了export import myThings = myThings;
    【解决方案3】:

    你可以试试:

    export {IBar} from '../bar.d.ts'
    export {IFoo} from '../foo.d.ts'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-31
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 2022-09-24
      • 2020-05-04
      • 1970-01-01
      • 2014-09-05
      相关资源
      最近更新 更多