Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 使导出的类全局可见_Typescript - Fatal编程技术网

Typescript 使导出的类全局可见

Typescript 使导出的类全局可见,typescript,Typescript,我在模块内定义了一个类,并将其导出为默认值,如下所示: // file: Component.ts import UIComponent from "path/to/UIComponent"; namespace typescript.example.app { export class Component extends UIComponent { ... } } export default typescript.example.app.Comp

我在模块内定义了一个类,并将其导出为默认值,如下所示:

// file: Component.ts
import UIComponent  from "path/to/UIComponent";

namespace typescript.example.app
{
    export class Component extends UIComponent
    {
        ...
    }
}

export default typescript.example.app.Component;
在另一个文件中,除非我想在运行时使用组件类(创建实例或调用静态方法),否则我不需要导入它

// file: UseComponent.ts
namespace typescript.example.app
{
    export class UseComponent
    {
        ...
        // error: namespace typescript.example.app has no exported member Component
        public myMethod(component: typescript.example.app.Component) { ... }
        ...
    }
}

export default typescript.example.app.UseComponent;
如何使
typescript.example.app.Component
在模块内声明时全局可见?

名称空间是typescript保留的非标准功能,目的是误导初学者。仅使用ES6模块:

// file: Component.ts
import UIComponent  from "path/to/UIComponent";

export default class Component extends UIComponent {
  // ...
}
然后:

// file: UseComponent.ts
import Component from "./Component";

export default class UseComponent {
  public myMethod(component: Component) {
  }
}
另见:

  • 第二
  • )
  • )

导出
命名空间
。尝试放入
导出命名空间类型脚本导出默认typescript.example.app.Component之前的code>并出现此错误:“合并声明'typescript'中的单个声明必须全部导出或全部本地。”我正在使用框架,需要将类导出为默认值。不要使用任何“名称空间”。只需使用ES6模块。这正是我不想做的。如果编译后不需要模块,为什么要导入它?它唯一的用途是在参数的类型中,在typescript将其转换为javascript后,它将不再存在于我的代码中…@lmcarreiro typescript不会生成
导入
,如果它在javascript中不需要。你可以试试。我相信,如果这是正确的方法,我们就去做吧。谢谢