Duplicate identifier error when referencing a node library using typescript

允我心安 提交于 2019-12-24 03:33:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!