如何在一个文件中导入所有类,并从那里使用打字稿

Zac*_*lic 3 node.js typescript

例如,我有一个班级 A.ts

export class A(){
    public constructor() {}
    public method() : string {
         return "hello from A";
    }
}
Run Code Online (Sandbox Code Playgroud)

和班级 B.ts

export class B(){
    public constructor() {}

    public method() : string {
         return "hello from B";
    }

    public static methodStatic() : string {
        return "hello from static B";
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我想在一个文件Headers.ts中导入所有这些

imports {A} from    "../A_path";
imports * as b from "../B_path";

//Or just to export it? Make it public.
export * from "../A_path";
export * from "../B_path";
Run Code Online (Sandbox Code Playgroud)

Headers.ts只包含imports/exports,没有类实现或任何其他代码.然后我的问题:我想在app.ts中使用AB调用它们在Headers.ts文件中调用它们.

import * as headers from './HEADERS_path';
headers.A.method();
headers.B.method();
Run Code Online (Sandbox Code Playgroud)

这该怎么做?

Zac*_*lic 8

好的我找到了解决方案:在Headers.ts文件中我只需要导出类:

export {A} from "../A_path";
export {B} from "../B_path";
Run Code Online (Sandbox Code Playgroud)

然后使用B类,我需要在app.ts文件中导入 Headers.ts:

import * as headers from './HEADERS_path';
Run Code Online (Sandbox Code Playgroud)

然后我将创建一个B类的实例并轻松地调用它的方法:

let bInstance : headers.B = new headers.B();
//concrete method
bInstance.method();
//output will be what is expected: *"hello from B"*

//static method
headers.B.methodStatic();
//output will be what is expected: *"hello from static B"*
Run Code Online (Sandbox Code Playgroud)