javascript - 使用 TypeScript 引用节点库时出现重复标识符错误
我刚接触 TypeScript,想开始一个 hapi.js 项目,但是在我的代码中尝试 require('boom') 时出现错误
Duplicate identifier 'Boom'/// <reference path="../typings/tsd.d.ts" />
var config = require('../config'),
User = require('../models/user'),
Boom = require('boom'),
joi = require('joi');
我的 tsd.json
{
"version": "v4",
"repo": "borisyankov/DefinitelyTyped",
"ref": "master",
"path": "typings",
"bundle": "typings/tsd.d.ts",
"installed": {
"node/node.d.ts": {
"commit": "846a250e0a6f5e6adf6347ee4ca442a9d1abd8fc"
},
"hapi/hapi.d.ts": {
"commit": "846a250e0a6f5e6adf6347ee4ca442a9d1abd8fc"
},
"bluebird/bluebird.d.ts": {
"commit": "846a250e0a6f5e6adf6347ee4ca442a9d1abd8fc"
},
"boom/boom.d.ts": {
"commit": "846a250e0a6f5e6adf6347ee4ca442a9d1abd8fc"
},
"pg/pg.d.ts": {
"commit": "846a250e0a6f5e6adf6347ee4ca442a9d1abd8fc"
}
}
我已经将 boom 和 hapi 都添加到我的 tsd 中——如果 hapi 也引用了 boom,那会导致某种循环错误吗?
我应该如何在 TypeScript 中引用这些库?
答案#1
如果你没有在文件中放置全局 import 或 export,那么在 TypeScript 看来,你的文件是 全局 命名空间的一部分,因此你会在 Boom 上发生名称冲突。解决方法:使用 import/require 替代 var/require。
import Boom = require('boom');
了解更多: https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1
