简体   繁体   English

在Typescript中使用点符号进行多次导出

[英]Multiple exports with dot notation in Typescript

Ref. 参考。 this question . 这个问题

I'm trying to do exactly the same, but with Typescript. 我正在尝试完全相同,但使用Typescript。

Problem 问题

This line (just before the export statement): 此行(仅在导出语句之前):

Sidebar.Item = SidebarItem;

gives compile error: Property 'Item' does not exist on type 'typeof Sidebar' 给出编译错误: Property 'Item' does not exist on type 'typeof Sidebar'

Attempted solution 1 尝试的解决方案1

I've tried adding Item to my Sidebar class definition, like so: 我尝试将Item添加到我的Sidebar类定义中,如下所示:

class Sidebar extends Component<SidebarProps, SidebarState> {
    Item: SidebarItem;
    ...
}

But that results in this error: Public property 'Item' of exported class has or is using private name 'SidebarItem'. 但这导致此错误: Public property 'Item' of exported class has or is using private name 'SidebarItem'.

Attempted solution 2 尝试的解决方案2

I've tried removing this line: 我试过删除此行:

Sidebar.Item = SidebarItem;

And instead of adding Item to my class definition, I'm exporting both classes like so: 而不是将Item添加到我的类定义中,而是像这样导出两个类:

export default Sidebar;
export { SidebarItem as Item };

Which does compile, and I am able to import everything like so: 确实可以编译,并且我可以像这样导入所有内容:

import * as Sidebar from '...';

But then I have to use them like so: 但是然后我必须像这样使用它们:

<Sidebar.default>
    <Sidebar.Item></Sidebar.Item>
    <Sidebar.Item></Sidebar.Item>
</Sidebar.default>

And I really want to get rid of that .default in Sidebar . 我真的想摆脱Sidebar中的.default

What am I doing wrong here? 我在这里做错了什么?

This module is faulty by design. 该模块设计有故障。

It likely can be done with: 可以用以下方法完成:

export class SidebarItem ...

export default class Sidebar extends Component<SidebarProps, SidebarState> {
    static Item = SidebarItem;
    ...
}

And there is no good reason why these two components should maintain a hierarchy like that. 而且,没有充分的理由说明为什么这两个组件应该保持这样的层次结构。

It's conventional to have CamelCased components and use separate named exports if there's more than one export of equal value (which components are). 如果有多个同等价值的出口(这些组件是),通常具有CamelCased组件并使用单独的命名出口。 Named exports are better for auto-import in IDEs: 命名导出更适合在IDE中自动导入:

export { Sidebar, SidebarItem };

Item cannot be tree-shaken from default export when needed. Item时,不能从默认导出中摇晃Item Sidebar.Item minifies less efficiently than SidebarItem . Sidebar.Item缩小效率不如SidebarItem

Your first solution is almost correct, but you declare Item as a property on objects of the Sidebar class, rather than on the class itself. 您的第一个解决方案几乎是正确的,但是您将Item声明为Sidebar类的对象的属性,而不是类本身的属性。 You can do the latter with a static property declaration. 您可以使用静态属性声明来完成后者。 This should work: 这应该工作:

export class SidebarItem ..
..
class Sidebar extends Component<SidebarProps, SidebarState> {
  static Item = SidebarItem;
..
export default Sidebar;

You won't need the line Sidebar.Item = SidebarItem; 您将不需要Sidebar.Item = SidebarItem;Sidebar.Item = SidebarItem; anymore, but you might need to move the SidebarItem class so it appears before Sidebar . 不再,但是您可能需要移动SidebarItem类,使其出现在Sidebar之前。 The import will just be import Sidebar from '...' . 导入将只是import Sidebar from '...'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM