Export namespace with other name in TypeScript

128 views Asked by At

In below example, I want export merged class ConsoleCommandsParser and namespace ConsoleCommandParserTypes as ConsoleCommandsParser. 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;
1

There are 1 answers

1
Vishal Petkar On

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.