从 TypeScript 项目中的一个文件中导出所有接口/类型 [英] Export all interfaces/types from one file in TypeScript project

查看:90
本文介绍了从 TypeScript 项目中的一个文件中导出所有接口/类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的库的直接接口被捕获

The direct interface to my library is captured by

index.d.ts

这是当前自动生成的

index.ts

在我的项目的 package.json 文件中,typings/types 属性指向这个 index.d.ts 文件,这是我的理解,它应该是怎样的.到目前为止一切都很好.

in my package.json file for my project, typings/types properties point to this index.d.ts file, which is to my understanding, how it should be. All good so far.

但是,还有一些我想从 index.d.ts 中导出的类型,这些类型目前在 index.d.ts 中没有表达.

However, there are some more types that I would like to export from index.d.ts that are not currently expressed in index.d.ts.

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

For example I have some manually created types in

foo.d.ts
bar.d.ts

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

and I want these to be expressed in index.d.ts.

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

The only way I think that might work, is to import the foo.d.ts/bar.d.ts types into index.d.ts and then in turn export them from that file.

但是,我遇到了麻烦!

这是我尝试过的:

// 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

这可能吗?我该怎么做?

is this possible? How do I do this?

推荐答案

如果不需要使用,可以直接导出.

You can export them directly if you don't need to use them.

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

或者如果您只需要导出其中的一些,您可以使用.

Or if you need to export only some of them you can use.

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

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

Or if you want to rename them during export you can do:

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

您可以在 MDN 文档中阅读有关它们的更多信息 这里.

You can read more about them in the MDN documentation here.

这篇关于从 TypeScript 项目中的一个文件中导出所有接口/类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆