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.
Best Answer
If you've export
ed 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.
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.