0

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?

lmcarreiro
  • 5,312
  • 7
  • 36
  • 63

1 Answers1

1

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:

Paleo
  • 21,831
  • 4
  • 65
  • 76
  • This is exactly what I don't wanna do. Why import a module if I won't need it after compilation? Its only use is in a argument's type, after typescript transpile it into javascript, it won't exist in my code anymore... – lmcarreiro Oct 09 '17 at 01:21
  • @lmcarreiro TypeScript doesn't generate the `import` if it is not needed in JavaScript. You can try. – Paleo Oct 09 '17 at 07:14
  • I'm convinced, if this is the right way, let's do it. Thanks. – lmcarreiro Oct 09 '17 at 09:52