Typescript: Cant export class from a module

Created on 19 May 2015  路  8Comments  路  Source: microsoft/TypeScript

I am using typescript 1.5 and common js modules type and when i declare my class inside module i get this error:

client/js/main.ts(4,9): Error TS2305: Module '"client/js/routeController"' has no exported member 'RouteController'.

routeController:


export module myApp {
  export class RouteController {
    $router: any;
    constructor($router) {
      this.$router = $router;

      $router.config([
        { path: '/', component: 'home' }
      ]);
    }
  }
}

main:

import {RouteController} from './routeController';

But when i remove the module and doing this:

class RouteController {
  $router: any;
  constructor($router) {
    this.$router = $router;

    $router.config([
      { path: '/', component: 'home' }
    ]);
  }
}

export default RouteController;

this is working. what is the problem?

Question

Most helpful comment

Try to put an export instruction after your module, like this export = services;. More detailed example:

module services {
    export class TestService {
        constructor() {
            console.log("TestService: new");
        }
    }
    export class TestService2 {
        constructor() {
            console.log("TestService2: new");
        }
    }
}
export = services;

Then you can import those classes using a typical syntax: import {TestService} from 'services';. Also there is the tsproject, very usefull for bundling.

All 8 comments

The object you're exporting is called myApp.RouteController.

I would just remove the myApp module declaration entirely and have the export class RouteController at top-level -- why have a container with only object in it?

Because i want module with many controllers in seperate files so how you suggest to do this?

I would recommend reading this answer: http://stackoverflow.com/questions/30176289/using-external-modules-in-typescript/30176523#30176523

A module that spans multiple external modules is not _one_ module, it's a bunch of different modules that all happen to have the same name. The only thing you gain by doing this is to add an extra level of indirection to consumers of the classes contained in those separate modules.

My goal is to do something like this:

myApp.RouteController = ...
myApp.otherController = ...

in this docs https://github.com/Microsoft/TypeScript/issues/2242 i see that you can do like i did my code. so i does not understand why i get the error?

Try to put an export instruction after your module, like this export = services;. More detailed example:

module services {
    export class TestService {
        constructor() {
            console.log("TestService: new");
        }
    }
    export class TestService2 {
        constructor() {
            console.log("TestService2: new");
        }
    }
}
export = services;

Then you can import those classes using a typical syntax: import {TestService} from 'services';. Also there is the tsproject, very usefull for bundling.

looks like this issue has been addressed. please reopen if there more information needed.

@C4Grey, thank you, that's works. But why it is not the same as:

export module services {
    export class TestService {
    }
}

I came from C++ and PHP and do not understand namespaces and importing namespaces. Seems it is better not to use namespaces at current time.

Just for the records:

import {RouteController} from './routeController';
works fine for
export class RouteController

but for
export default RouteController
you would need
import RouteController from './routeController';

So, no curly brackets when exporting a default.
I know, the error message can be misleading if it says "export 'RouteController' was not found in './blabla'".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tenry92 picture tenry92  路  146Comments

Gaelan picture Gaelan  路  231Comments

rbuckton picture rbuckton  路  139Comments

RyanCavanaugh picture RyanCavanaugh  路  205Comments

yortus picture yortus  路  157Comments