lmcarreiro
lmcarreiro

Reputation: 5792

Make a exported class visible globally

I've defined a class inside a module and exported it as default, like this:

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

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

export default typescript.example.app.Component;

In another file, unless I want to use the Component class at runtime (create an instance or call a static method), I don't need to import it.

// 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;

How can I make typescript.example.app.Component globally visible with it being declared inside a module?

Upvotes: 0

Views: 1413

Answers (1)

Paleo
Paleo

Reputation: 23772

Namespaces are a non-standard feature that TypeScript keeps in order to mislead beginners. Use ES6 modules only:

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

export default class Component extends UIComponent {
  // ...
}

And then:

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

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

See also:

Upvotes: 1

Related Questions