使用TypeScript引用节点库时出现重复标识符错误 [英] Duplicate identifier error when referencing a node library using typescript

查看:150
本文介绍了使用TypeScript引用节点库时出现重复标识符错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是打字稿的新手,试图启动hapi.js项目,但是在我的代码中尝试require('boom')时遇到错误

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');

我的tsd.json

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

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

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?

推荐答案

如果您没有在文件中放置全局importexport,那么就TypeScript而言,您的文件是 global 名称空间,因此您在Boom上遇到名称冲突.修复:使用import/require而不是var/require.

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');

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

这篇关于使用TypeScript引用节点库时出现重复标识符错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆