简体   繁体   English

以 TypeScript 中的其他名称导出命名空间

[英]Export namespace with other name in TypeScript

In below example, I want export merged class ConsoleCommandsParser and namespace ConsoleCommandParserTypes as ConsoleCommandsParser .在下面的示例中,我希望将合并的 class ConsoleCommandsParser和命名空间ConsoleCommandParserTypesConsoleCommandsParser Easy, it both declared in one file, but here is other case.很简单,它都在一个文件中声明,但这是另一种情况。

import ConsoleCommandParserTypes from "./ConsoleCommandParserTypes";

export abstract class ConsoleCommandsParser {

  public static parse(arrayedConsoleCommand: Array<string>): void {
    // not implemented yet
  }
}

// Invalid syntax
export namespace ConsoleCommandParserTypes as ConsoleCommandParser;
// Namespace can not be used as value
export ConsoleCommandParser = ConsoleCommandParserTypes 
// Invalid syntax
export namespace ConsoleCommandParser = ConsoleCommandParserTypes;

Try the below code, I have tested it in my type script file试试下面的代码,我已经在我的类型脚本文件中测试过了

namespace Test {
  export class Test1 {
    static xyz(xyz: any) {
      throw new Error("Method not implemented.");
    }
    xyz:string = "XYZ";
  }
  export class Test2 {
    abc:string = "ABC";
  }
}

// Use this to create an object and access the methods and variables, it works for me
let test1 = new Test.Test1;
console.log(test1.xyz);

Let me know if this will not work for you.让我知道这是否对您不起作用。

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

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