![](/img/trans.png)
[英]How to declare a module in TypeScript with an object as default export
[英]How can I export a default object with interfaces in it in TypeScript?
import A from './A';
import type { AProps } from './A';
import B from './B';
import type { BProps } from './B';
export default {
A, AProps,
B, BProps
}
我想导出一个默认的 object,里面有interfaces
。
上面的代码在vite下出现异常是因为output js文件中没有定义AProps
和BProps
变量。
我尝试了另一种可以解决这个问题的方法:
export A from './A';
export type { AProps } from './A';
export B from './B';
export type { BProps } from './B';
并使用import * as X from './xx'
。 但我更喜欢使用import X from './xx'
,这意味着我必须从文件中导出默认值。
然后我创建了另一个文件,其中代码export * as default from './xx'
in it and it's working well.
我的问题是我是否可以将所有export
代码放在一个文件中?
您注意到您可以使用:
export A from './A';
export type { AProps } from './A';
export B from './B';
export type { BProps } from './B';
但是你必须使用通配符导入:
import * as X from './xx'
但是你有没有注意到X
是一个命名空间? 也许您可以创建自己的命名空间...请考虑以下事项:
import _A from './A';
import type { AProps as _AProps } from './A';
import _B from './B';
import type { BProps as _BProps } from './B';
namespace Module {
export const A = _A;
export const B = _B;
export type AProps = _AProps;
export type BProps = _BProps;
}
export default Module;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.