0

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

我的.tsx文件(Typescript + JSX gulp-typescript)在我使用import * as React from 'react'. 但是,当我使用require语句时出现错误:

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

如果我只是导入错误并且新旧之间存在一些我尚未摸索的差异requireimport我不会感到惊讶,但我认为我得到的特定类型冲突错误很奇怪。

只是为了更好地理解导入如何与全局 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" />
4

1 回答 1

0

反应全局.d.ts

不要安装反应全局。我很高兴看到你已经在使用模块(更多https://github.com/basarat/typescript-book/blob/master/docs/tips/outFile.md)所以使用 install react 因为typings install dt~react --save它将安装你的模块 react那么可以import * as React from "react";

于 2016-06-01T01:41:07.320 回答