samanime
samanime

Reputation: 26567

TypeScript "use"/alias for Classes

Is there a way to do an alias or "use" (like PHP) for a TypeScript class/module.

Example:

If I have:

module Foo {
    class Bar {}
}

Normally I have to write Foo.Bar to use it outside of the module. Is there a way I can alias that to something else, like "FooBar".

This would be really useful if you have several submodules (which my current project does), like:

module A.B.C.D { export class E {} }

is normally A.B.C.D.E which is silly.

Upvotes: 3

Views: 3927

Answers (1)

Dax70
Dax70

Reputation: 1051

According to page 82 of the old Typescript language spec, it stated that the following is possible. So you should be able to alias "use" a module without having to reference the entire hierarchy.

module A.B.C
{
  import XYZ = X.Y.Z;

  export function ping(x: number) {
    if (x > 0) XYZ.pong(x – 1);
  }
}

module X.Y.Z
{
  import ABC = A.B.C;

  export function pong(x: number) {
    if (x > 0) ABC.ping(x – 1);
  }
}

The new Typescript language spec covers similar material in the section on Import Alias Declarations

Upvotes: 6

Related Questions