问题
I'm new to typescript and trying to start a hapi.js project but I'm getting an error when trying require('boom') in my code
Duplicate identifier 'Boom'
/// <reference path="../typings/tsd.d.ts" />
var config = require('../config'),
User = require('../models/user'),
Boom = require('boom'),
joi = require('joi');
my 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"
}
}
I've added both boom and hapi to my tsd - if hapi references boom as well does that cause some sort of circular error?
How should I be referencing these libraries in typescript?
回答1:
If you don't put a global import
or export
in your file then as far as TypeScript is concerned you file is a part of a global namespace and therefore you are getting a name collision on Boom
. Fix: use import/require
instead of var/require
.
import Boom = require('boom');
To learn more : https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1
来源:https://stackoverflow.com/questions/30201985/duplicate-identifier-error-when-referencing-a-node-library-using-typescript