gpt4 book ai didi

typescript - 导出所有导入的接口(interface)

转载 作者:搜寻专家 更新时间:2023-10-30 21:59:21 32 4
gpt4 key购买 nike

我正在编写一个提供类的 NPM 包。在这个类中,我使用了我在“types.ts”文件中定义的多个接口(interface)。不幸的是,当我想自己使用这个包时,我无法访问这些接口(interface)。我认为这是因为我需要导出导入的接口(interface)。经过一番研究后,我注意到有 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();

我的问题:

如何让我的库的接口(interface)对导入该类的其他人公开?

最佳答案

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

搜索再导出。

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

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

关于typescript - 导出所有导入的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47221800/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com