This Question is Answered

2 "helpful" answers available (2 pts)
2 Replies Last post: Oct 6, 2013 8:01 PM by Trygve Botnen  
Trygve Botnen Newbie 4 posts since
Jun 26, 2011
Currently Being Moderated

Oct 6, 2013 12:32 PM

Typescript modules

Hi,

I am trying to learn Typescript, and have difficulties to find good documentation for a complete beginner.

 

I would like to put my code into modules, normally one class per module. I thought  it would be this simple:

 

I create two files, abc.ts and def.ts,  each file defines a class, and class Def will use class Abc.

 

File abc:

 

module abc {
    export class Abc {
       name: string;
       constructor(name: string) {
          this.name = name;
       }
        getName= function() {
           return this.name;
        }
    }
}

 

File def.ts

 

import a = require("abc");
module def {
    export class Def {
       name: string;
       myAbc:a.Abc;
       constructor(name: string) {
          this.myAbc= new a.Abc("foo");
          this.name = this.myAbc.getName();
       }
       getName= function() {
          return this.name;
       }
    }
}

 

However, Webstorm (7.0) will not find the file abc.ts when def.ts is compiled They are both in the same directory.

 

I get the following errors:

myDir/js/def.ts(2,1): error TS2071: Unable to resolve external module '"abc"'.

myDir/js/def.ts(2,1): error TS2072: Module cannot be aliased to a non-module type.

 

error TS5037: Cannot compile external modules unless the '--module' flag is provided.

I don't understand the second error code TS2072

The last error code seems to give a hint: How do I set the module flag?

 

I have tried adding /// <reference path="./abc.ts" /> at the top of file def.ts, with no improvement.

 

Any help is welcome (also links to good newbie documentations)!

Jaime Bruton Newbie 1 posts since
Oct 6, 2013
Currently Being Moderated
Oct 6, 2013 7:27 PM in response to: Trygve Botnen
Re: Typescript modules

Have you tried this?

 

def.ts

 

/// <reference path="abc.ts" />

 

module def {

    export class Def {

        name: string;

        myAbc: abc.Abc;

 

        constructor(name: string) {

            this.myAbc = new abc.Abc(name);

            this.name = this.myAbc.getName();

        }

        getName= function() {

            return this.name;

        };

    }

}

More Like This

  • Retrieving data ...