Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript cannot find name even though it is referenced

Tags:

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.

like image 554
Gilthans Avatar asked Feb 20 '15 20:02

Gilthans


People also ask

How to fix Cannot find name in TypeScript?

To solve the error "Cannot find name module", install the node types by running npm i -D @types/node . If the error is not resolved, try adding node to your types array in tsconfig. json and restarting your IDE.

Can not find module path?

To solve the "Cannot find module path or its corresponding type declarations" error, install the types for node by running the command npm i -D @types/node . You can then import path with the following line of code import * as path from 'path' .

What is require in angular?

AngularJS ng-required DirectiveThe ng-required directive sets the required attribute of a form field (input or textarea). The form field will be required if the expression inside the ng-required attribute returns true. The ng-required directive is necessary to be able to shift the value between true and false .


Video Answer


2 Answers

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.

like image 174
Ryan Cavanaugh Avatar answered Sep 21 '22 00:09

Ryan Cavanaugh


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.

like image 30
Gilthans Avatar answered Sep 21 '22 00:09

Gilthans