无法在同一个TypeScript项目中使用RequireJS和Node的要求 [英] Unable to use requireJS and Node's Require in the same TypeScript project
本文介绍了无法在同一个TypeScript项目中使用RequireJS和Node的要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个同时面向Node和浏览器的TypeScript项目。我在一些脚本中使用Node的require()
,在其他脚本中使用JS的require()
。我的项目目录如下所示:
myProject
-- a.ts
-- b.ts
-- node.d.ts
-- require.d.ts
其中a.ts
包含:
/// <reference path="./node.d.ts" />
var cp = require('child-process');
var a = 'hello world'
export = a
和b.ts
包含:
/// <reference path="./require.d.ts" />
require('hello',function(x){console.log('world')});
var b = 'hello world'
export = b
其中require.d.ts
和node.d.ts
取自DefinitlyTyped。
当我编译项目时,收到以下错误:
b.ts(2,1): error TS2346: Supplied parameters do not match any signature of call target.
require.d.ts(396,13): error TS2403: Subsequent variable declarations must have the same type. Variable 'require' must be of type 'NodeRequire', but here has type 'Require'.
我使用此习惯用法来确定要加载哪些模块,因此我不会在浏览器中加载节点模块,反之亦然。
if(typeof module !== 'undefined' && module.exports){
// We're in a Node process
}else{
// We're in an AMD module in the browser:
}
是否有办法在同一项目中同时使用这两个.d.ts
文件。似乎在单独的模块中使用它们是不够的。
推荐答案
有没有办法在同一项目中同时使用这两个.d.ts文件
我强烈建议您在任何地方都使用commonjs
。这就是Reaction社区所倡导的,而且它的工作流程要简单得多。只需使用CommonJS+webpack(从here开始变懒require.ensure
)即可。🌹
还有TypeScript in the browser environment的快速入门。
这篇关于无法在同一个TypeScript项目中使用RequireJS和Node的要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文