简体   繁体   English

在打字稿中引用外部模块-错误TS2304:找不到名称“通用”

[英]Referencing external module in typescript - error TS2304: Cannot find name 'general'

I prepared these two files: 我准备了这两个文件:

1st one is general.d.ts file 第一个是general.d.ts文件

interface IgeneralStatic {
    General: {
        Langs: any;
    };
}

declare var general: IgeneralStatic;

declare module 'general' {
    export = general;
}

2nd one is just something.ts file where i'm trying to import a file alias: 第二个只是我试图导入文件别名的something.ts文件:

/// <reference path="general.d.ts" />

import general = require('general');

export class SpecificLangs extends general.General.Langs
  ...
}

When i trying to compile it i'm getting this error: 当我尝试编译它时,出现此错误:

error TS2304: Cannot find name 'general'

The reason of this that I don't know where is my module and I can't import it for typescript usage. 原因是我不知道模块在哪里,也无法导入它以供打字稿使用。 It is resolved on requirejs side as package. 它在requirejs方面作为软件包解决。 So there is no way to reference, using import, original general.ts file. 因此,无法使用导入的原始general.ts文件进行引用。

Your definition works, in that you can import it... 您的定义有效,因为您可以导入它...

For example, I can reference Langs like so: 例如,我可以像这样引用Langs

var x = general.General.Langs;

However, you haven't declared that Langs is a class, so you can't extend it as if it were one. 但是,您尚未声明Langs是一类,因此您无法像将其那样扩展它。

If it is implemented in a way that allows you to extend it, declaring it as a class will allow it to be used as a base class (simplified example to demonstrate that using a class works): 如果一种方式,可以扩展它来实现,它声明为一个类将允许它被用作基类(简化的示例,以证明使用一个类的作品):

declare module 'general' {
    export module General {
        export class Langs {


        }
    }
}

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

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