简体   繁体   English

带有requirejs和许多路径别名和软件包的gulp-typescript,错误TS2307

[英]gulp-typescript with requirejs and many path aliases and packages, error TS2307

I have a large project that is mostly JavaScript. 我有一个大型项目,主要是JavaScript。 I would like to add some TypeScript. 我想添加一些TypeScript。 Things are mostly working fine. 事情基本上都很好。 However, I have a large number of paths aliases and packages. 但是,我有大量的路径别名和包。 So, when I 所以,当我

 import foo = require('foo');

This will, in runtime, rely on a path configuration, say 例如,这将在运行时依赖于路径配置

 require.config({
     paths: {
         foo: 'foobar/baz/js/quux'
     }
 });

So, predictably, the compiler gives me: 因此,可以预见的是,编译器给了我:

error TS2307: Cannot find module 'foo'

Is there any way that I could load in my requirejs configuration so that the compiler will be happy? 有什么方法可以加载到我的requirejs配置中,以便编译器满意吗? Alternatively, is there a way that I suppress/ignore the error? 另外,有没有一种方法可以抑制/忽略该错误? The output JavaScript looks and runs fine. 输出的JavaScript看起来并运行良好。 If I suppress the error, I wonder what I would be losing ... Can I specify where to find all my modules? 如果我抑制了该错误,我想知道我会损失什么?我可以指定在哪里找到所有模块吗?

You can declare your own module in a definition file: 您可以在定义文件中声明自己的模块:

// my-modules.d.ts
declare module "foo" {
}

This way, gulp-typescript will not complain as long as the my-modules.d.ts file compiles with the rest. 这样,只要my-modules.d.ts文件与其余文件一起编译,gulp-typescript就不会抱怨。

is there any way that I could load in my requirejs configuration so that the compiler will be happy? 有什么方法可以加载到我的requirejs配置中,以便编译器满意吗?

Not yet. 还没。 Look out for https://github.com/Microsoft/TypeScript/issues/5039 请注意https://github.com/Microsoft/TypeScript/issues/5039

Alternatively, is there a way that I suppress/ignore the error? 另外,有没有一种方法可以抑制/忽略该错误?

Only if you want to be completely unsafe aka any : 仅当您想完全不安全时也any

var foo:any = require('foo');

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

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