为什么要求('反应')导致"导入声明冲突"打字稿?

时间:2016-05-31 16:39:56

标签: javascript reactjs typescript jsx tsd

我正在尝试将Typescript与tsd(尚未升级到typings),React和JSX一起使用......哦,我的!谈谈构建系统的复杂性......

当我使用.tsx时,我的gulp-typescript文件(Typescript + JSX)编译得很好(使用import * as React from 'react')。但是,当我使用require语句时,我收到错误:

./typings/react/react-global.d.ts(17,1): error TS2440: Import declaration conflicts 
with local declaration of 'React'

如果我输入错误并且旧的require和新import之间存在一些我还没有理解的差异,我不会感到惊讶,但我想我得到的特殊类型冲突错误很奇怪。

我理解导入如何更好地与全局tsd声明进行交互,为什么require语句会导致错误?

这是我的index.tsx

/// <reference path="../typings/tsd.d.ts" />

const config = require('../config')
const express = require('express')
const reactDomServer = require('react-dom/server')
const app = express()

// THIS WORKS
import * as React from 'react'

// THIS DOES NOT
//const React = require('react')

app.get('/', (req, res) => {
  const element = <h1>Hello Denver!</h1>
  res.send(reactDomServer.renderToString(element))
})

const server = app.listen(process.env.port || config.port, () =>
  console.log(`Server listening on port ${server.address().port}!`)
)

这是我的../typings/tsd.d.ts

/// <reference path="react-router/history.d.ts" />
/// <reference path="react-router/react-router.d.ts" />
/// <reference path="react/react-addons-create-fragment.d.ts" />
/// <reference path="react/react-addons-css-transition-group.d.ts" />
/// <reference path="react/react-addons-linked-state-mixin.d.ts" />
/// <reference path="react/react-addons-perf.d.ts" />
/// <reference path="react/react-addons-pure-render-mixin.d.ts" />
/// <reference path="react/react-addons-test-utils.d.ts" />
/// <reference path="react/react-addons-transition-group.d.ts" />
/// <reference path="react/react-addons-update.d.ts" />
/// <reference path="react/react-dom.d.ts" />
/// <reference path="react/react-global.d.ts" />
/// <reference path="react/react.d.ts" />
/// <reference path="node/node.d.ts" />
/// <reference path="express-serve-static-core/express-serve-static-core.d.ts" />
/// <reference path="express/express.d.ts" />
/// <reference path="mime/mime.d.ts" />
/// <reference path="serve-static/serve-static.d.ts" />

1 个答案:

答案 0 :(得分:0)

  

反应-global.d.ts

不要安装全球反应。我很高兴看到你已经使用了模块(更多https://github.com/basarat/typescript-book/blob/master/docs/tips/outFile.md)所以使用install react typings install dt~react --save将安装模块做出反应,然后import * as React from "react";