【发布时间】:2016-10-27 06:34:12
【问题描述】:
我有一个针对 Node 和浏览器的打字稿项目。我在一些脚本中使用 Node 的 require() 并在其他脚本中使用 requireJS 的 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 文件。似乎在单独的模块中使用它们是不够的。
【问题讨论】:
标签: node.js typescript requirejs