【问题标题】:TypeScript+React suddenly giving incompatible Props errorsTypeScript+React 突然给出不兼容的 Props 错误
【发布时间】:2018-04-05 12:04:07
【问题描述】:

昨天,我的 TypeScript+React 项目一切正常。今天一切都被打破了,任何配置都没有改变。使用 Git 将其回滚到工作版本并没有帮助。使用 NPM 重新安装所有软件包没有帮助。

我试图从 App.jsx 中删除所有内容以定位错误。

这是我的 app.jsx

import * as React from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';

interface Props {}

class App extends React.Component<Props> {
    render() {
        return (
            <div className="App">
                <Router>
                    <div>
                        <Route exacty={true} path="/" component={<p>Hello</p>}/>
                    </div>
                </Router>
            </div>
        );
    }
}

export default App;

但这仍然给我一个错误:

(10,5): error TS2605: JSX element type 'BrowserRouter' is not a constructor function for JSX elements.
Types of property 'setState' are incompatible.
Type '{ <K extends never>(f: (prevState: void, props: BrowserRouterProps) => Pick<void, K>, callback?: ...' is not assignable to type '{ <K extends never>(f: (prevState: {}, props: any) => Pick<{}, K>, callback?: () => any): void; <...'.
  Types of parameters 'f' and 'f' are incompatible.
    Type '(prevState: {}, props: any) => Pick<{}, any>' is not assignable to type '(prevState: void, props: BrowserRouterProps) => Pick<void, any>'.
      Types of parameters 'prevState' and 'prevState' are incompatible.
        Type 'void' is not assignable to type '{}'.

我遇到的问题是,每个带有 Props 的组件都被破坏了。例如,如果我有一个组件,例如我的 Header I get an error such as this 标头是一个带有空 Props 对象的组件。

如果有帮助,这是我的 tsconfig.json 和 package.json https://pastebin.com/FSyCA1BB

【问题讨论】:

  • 似乎 react-router-dom 的类型被破坏了:github.com/DefinitelyTyped/DefinitelyTyped/issues/17437
  • 好的。我改变了版本。现在它说命名空间 'React' 没有导出的成员 'AnchorHTMLAttributes' 并且重新安装所有 NPM 模块的建议修复没有做任何事情。

标签: javascript reactjs typescript jsx


【解决方案1】:

我遇到的问题是在 package.json 我有 @types/react-router-dom 。没有一个版本有效,但从 package.json 中完全删除它解决了问题。

【讨论】:

  • 它有效,但不知道是什么原因。应标记为正确答案。
  • react-router 可能会在其包本身中添加类型。而不是将@types/react-router 单独用于打字稿类型。
【解决方案2】:

react-router 的类型定义经常过期/无效。维护类型定义的问题与 react-router 的文档同样频繁地过时有关。

修复

等到定义赶上来

备用

我写了一些简单的东西来解决它http://basarat.com/takeme

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2022-01-14
    • 2019-01-07
    • 2022-01-08
    • 2013-08-20
    相关资源
    最近更新 更多