【问题标题】:Export all imported interfaces导出所有导入的接口
【发布时间】:2017-11-10 19:43:46
【问题描述】:

我正在编写一个提供类的 NPM 包。在这个类中,我使用了我在“types.ts”文件中定义的多个接口。不幸的是,当我想自己使用包时,我无法访问这些接口。我认为这是因为我需要导出导入的接口。经过一番研究,我注意到有export import { IMyInterface } from './common/types',但它似乎已经过时/不推荐使用。

我的 index.ts:

import { IMyInterface, IMyInterface2 } from './common/types';

export class ExampleClass {
  public async getInfo(): Promise<IMyInterface[]> {
    return await request();
  }
}

所以当我使用这个库时,我确实在 VSCode 中看到了返回的类型,但我无法使用这些类型定义变量。例如,这不起作用,因为找不到 IMyInterface

import { ExampleClass } from 'my-library'
const instance = new ExampleClass();
const info: IMyInterface = instance.getInfo();

我的问题:

如何将我的库接口公开给其他导入该类的人?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    您可以在您的 exampleClass 文件中重新导出您的接口。 link

    搜索再出口。

    export {IMyInterface as exampleInterface} from "./common/types";
    export {IMyInterface2 as exampleInterface2} from "./common/types";
    

    您可以在需要使用它们的地方导入新的示例接口。

    【讨论】:

    • 这正是我想要避免的。如果没有更好的方法,export import 方法似乎更好。那他们为什么要弃用它呢?您的方法的问题是我需要将导入的每个接口都添加到导出列表中。
    • 如果您想导出所有接口,您可以通过键入“*”而不是接口名称来实现。我假设您想以类型公开所有内容。
    • 理想情况下,我只会导出我也从types 导入的那些接口。
    猜你喜欢
    • 2015-08-23
    • 2017-05-31
    • 2017-04-12
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多