25

I have an angular project I'm writing in typescript. This worked well for me under VS, and now I'm trying the same with Node.JS under webstorm.

I have a progressor class, in a progressor.ts file:

export class Progressor{
    public tasks: any;

    constructor(){
        this.tasks = {};
    }
    ...
}

and I have my main controller, which uses this class:

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

declare var angular: any; // I'm trying to eliminate these...
declare var Papa: any;
declare var $: any;

class MainController{
    constructor($scope: any){
        $scope.progressor = null;
        $scope.filesDropped = function(files, rejectedFiles){
            if(!files || !files.length)
                return;

            $scope.progressor = new Progressor();
            // ...
        }
    };
}

Note that this is not Node.JS code - it is simply part of a Node.JS project.

The relative reference path is correct. When I try to compile it using:

tsc mainController.ts --module amd --target es5

I get an error: Cannot find name Progressor.

I don't understand what's the problem... I've been having nothing but trouble with my Node.JS project so far, and I'm considering giving up on TS altogether for this project. First of all - can anyone tell me why it won't compile? Note that I want each TS file to be compiled separately, so I can debug them comfortably via Chrome.

Gilthans
  • 1,656
  • 1
  • 18
  • 23

2 Answers2

13

If you've exported something, you need to import it in order to consume it, not <reference ... it.

Replace the <reference comment with import prog = require('./progressor');, then you can use e.g. new prog.Progressor().

You might consider using export = Progressor so that the exported object from the other file is the class itself instead of a container.

Ryan Cavanaugh
  • 209,514
  • 56
  • 272
  • 235
  • This works! Thanks a lot! But I don't understand, why isn't the good? What is the difference between that and 'import require'? I don't remember having to use it when I used TS under VS... – Gilthans Feb 20 '15 at 20:52
  • 2
    I take it back - the code now seems to have dependency for either requirejs or commonjs (I'm not sure), which is something I don't want and am pretty sure I don't have to have... – Gilthans Feb 20 '15 at 21:06
8

After a while more of searching, I stumbled upon this: TypeScript Modules. After consulting it, I tried placing both my classes inside module{ } blocks, which solved the problem. I'm still slightly confused as to why the language would require me to use modules for multi-file usage... but for now it will do.

Gilthans
  • 1,656
  • 1
  • 18
  • 23
  • 2
    If you're not using requirejs or commonjs modules, you can put your classes at top-level (i.e. not inside a module) and just not put the `export` keyword on them and everything will work like you want. – Ryan Cavanaugh Feb 20 '15 at 21:25
  • Isn't this exactly the case described in the original post? – Gilthans Feb 20 '15 at 21:26
  • The question is contradictory/confusing about whether or not you're actually nodejs as a module loader. – Ryan Cavanaugh Feb 20 '15 at 21:49
  • The project uses node.js for server-side, but this specific example is client-side code which runs at the browser (angular) – Gilthans Feb 20 '15 at 22:31
  • 1
    In that case you shouldn't have top-level `export` keywords (unless you're using requirejs) – Ryan Cavanaugh Feb 20 '15 at 23:25