TypeScript ES6 import module File is not a module error

0 votes

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?

Jun 3 in TypeSript by Logan
• 2,140 points
415 views

1 answer to this question.

0 votes

We need the export, as a part of the test.js file. Adjust the content of it like this:

// test.js - exporting es6
export module App {
  export class SomeClass {
    getName(): string {
      return 'name';
    }
  }
  export class OtherClass {
    getName(): string {
      return 'name';
    }
  }
}

And now we can import it with these three ways:

import * as app1 from "./test";
import app2 = require("./test");
import {App} from "./test";

And we can consume imported stuff like this:

var a1: app1.App.SomeClass  = new app1.App.SomeClass();
var a2: app1.App.OtherClass = new app1.App.OtherClass();

var b1: app2.App.SomeClass  = new app2.App.SomeClass();
var b2: app2.App.OtherClass = new app2.App.OtherClass();

var c1: App.SomeClass  = new App.SomeClass();
var c2: App.OtherClass = new App.OtherClass();

and call the method to see it in action:

console.log(a1.getName())
console.log(a2.getName())
console.log(b1.getName())
console.log(b2.getName())
console.log(c1.getName())
console.log(c2.getName())
answered Jun 7 by Nina
• 3,020 points

Related Questions In TypeSript

0 votes
1 answer
0 votes
1 answer

TypeScript Object assign gives me an error property assign does not exist on type ObjectConstructor

For TypeScript 2.1 and higher, you can ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
64 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

React Native Performance: Javascript vs Typescript

TypeScript is just compiled to JavaScript. Think ...READ MORE

answered Jun 13 in TypeSript by Nina
• 3,020 points
16 views
0 votes
1 answer

Wildcard module declaration not work in TypeScript 2, why?

I managed to resolve this by forcing ...READ MORE

answered Jun 16 in TypeSript by Nina
• 3,020 points
56 views
0 votes
1 answer

Using spread syntax and new Set() with typescript

This seems to be a typescript ES6 ...READ MORE

answered 5 days ago in TypeSript by Abhinaya
• 760 points
6 views
0 votes
1 answer

How to set meta tags using Angular universal SSR and ngx-seo plug-in?

first Install the plug-in with npm i ngx-seo ...READ MORE

answered Feb 11 in Others by narikkadan
• 11,280 points
688 views
0 votes
1 answer

What is "not assignable to parameter of type never" error in TypeScript?

All you have to do is define ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
501 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP