I am using typescript 1.6 with es6 modules syntax.
My files are:
test.ts:
module App {
export class SomeClass {
getName(): string {
return 'name';
}
}
}
main.ts:
import App from './test';
var a = new App.SomeClass();
When I am trying to compile the main.ts
file I get this error:
Error TS2306: File 'test.ts' is not a module.
How can I accomplish that?
Best Answer
Extended - to provide more details based on some comments
The error
Comes from the fact described here http://exploringjs.com/es6/ch_modules.html
Based on the above we need the
export
, as a part of the test.js file. Let's adjust the content of it like this:And now we can import it with these thre ways:
And we can consume imported stuff like this:
and call the method to see it in action:
Original part is trying to help to reduce the amount of complexity in usage of the namespace
Original part:
I would really strongly suggest to check this Q & A:
How do I use namespaces with TypeScript external modules?
Let me cite the first sentence:
In this case, we just do not need
module
inside oftest.ts
. This could be the content of it adjustedtest.ts
:Read more here
Export =
we can later consume it like this:
Read more here:
Optional Module Loading and Other Advanced Loading Scenarios