简体   繁体   English

即使被引用,Typescript也找不到名称

[英]Typescript cannot find name even though it is referenced

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. 这在VS下很适合我,现在我在webstorm下尝试使用Node.JS。

I have a progressor class, in a progressor.ts file: 我在progressor.ts文件中有一个进度类:

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. 请注意,这不是Node.JS代码 - 它只是Node.JS项目的一部分。

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. 我收到一个错误:找不到名称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. 我不明白是什么问题......到目前为止,我的Node.JS项目一直没有问题,而且我正在考虑完全放弃TS用于这个项目。 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. 请注意,我希望每个TS文件都单独编译,因此我可以通过Chrome轻松调试它们。

If you've export ed something, you need to import it in order to consume it, not <reference ... it. 如果您已export某些内容,则需要import它才能使用它,而不是<reference ... it。

Replace the <reference comment with import prog = require('./progressor'); import prog = require('./progressor');替换<reference comment import prog = require('./progressor'); , then you can use eg new prog.Progressor() . ,那么你可以使用例如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. 您可以考虑使用export = Progressor以便从另一个文件中导出的对象是类本身而不是容器。

After a while more of searching, I stumbled upon this: TypeScript Modules . 经过一段时间的搜索,我偶然发现了这个: 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. 我仍然有点困惑,为什么语言要求我使用模块进行多文件使用......但现在它会这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM