gpt4 book ai didi

javascript - TypeScript+React 突然出现不兼容的 Props 错误

转载 作者:搜寻专家 更新时间:2023-10-30 20:57:26 26 4
gpt4 key购买 nike

昨天,我的 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 header 是一个具有空 Props 对象的组件。

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

最佳答案

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

修复

等到定义跟上

备用

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

关于javascript - TypeScript+React 突然出现不兼容的 Props 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46914792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com