typescript 打字稿找不到名称,即使它被引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28637767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:35:40  来源:igfitidea点击:

Typescript cannot find name even though it is referenced

typescript

提问by Gilthans

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 类,在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. 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.

我不明白有什么问题......到目前为止,我的 Node.JS 项目遇到了一些麻烦,我正在考虑完全放弃 TS 来完成这个项目。首先 - 谁能告诉我为什么它不能编译?请注意,我希望单独编译每个 TS 文件,以便我可以通过 Chrome 轻松调试它们。

采纳答案by Gilthans

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.

经过一段时间的搜索,我偶然发现了这个:TypeScript Modules。在咨询之后,我尝试将我的两个类都放在 module{ } 块中,从而解决了问题。我仍然对为什么该语言要求我使用模块进行多文件使用感到有些困惑……但现在它会这样做。

回答by Ryan Cavanaugh

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

如果你已经export编辑了一些东西,你需要import它来消费它,而不是<reference ...它。

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

用 替换<reference注释import prog = require('./progressor');,然后您可以使用例如new prog.Progressor()

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

您可能会考虑使用,export = Progressor以便从另一个文件导出的对象是类本身而不是容器。