【问题标题】:Duplicate identifier error when referencing a node library using typescript使用打字稿引用节点库时出现重复标识符错误
【发布时间】:2015-07-23 23:35:31
【问题描述】:

我是 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"
    }
  }

我在我的 tsd 中添加了 boom 和 hapi - 如果 hapi 也引用了 boom 是否会导致某种循环错误?

我应该如何在 typescript 中引用这些库?

【问题讨论】:

标签: javascript typescript hapijs


【解决方案1】:

如果您没有将全局 importexport 放入文件中,那么就 TypeScript 而言,您的文件是 global 命名空间的一部分,因此您将获得一个Boom 上的名称冲突。修复:使用import/require 而不是var/require

import Boom = require('boom');

了解更多:https://www.youtube.com/watch?v=KDrWLMUY0R0&hd=1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-01
    • 2016-09-20
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 2015-09-28
    • 2020-04-22
    相关资源
    最近更新 更多