加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 具有react-dnd的TypeScript 2.0提供错误“JSX元素属

发布时间:2020-12-15 20:52:00 所属栏目:百科 来源:网络整理
导读:在一个TypeScript React项目中,我正在使用 react-dnd及其 DefinitelyTyped typings: interface ExampleScreenProps { a,b,c }interface ExampleScreenState { x,y,z }class ExampleScreen extends React.ComponentExampleScreenProps,ExampleScreenState {
在一个TypeScript React项目中,我正在使用 react-dnd及其 DefinitelyTyped typings:
interface ExampleScreenProps { a,b,c }
interface ExampleScreenState { x,y,z }

class ExampleScreen extends React.Component<ExampleScreenProps,ExampleScreenState> { }

export default DragDropContext(HTML5Backend)(ExampleScreen);

这被渲染在另一个组件中:

import ExampleScreen from "./ExampleScreen";

<ExampleScreen a="a" b="b" c="c" />

这在TS 1.8没有任何错误.当我升级到TS 2.0时,我得到以下编译错误:

Error:(90,10) TS2600: JSX element attributes type
‘(ExampleScreenProps & { children?: ReactNode; }) |
(ExampleScreenProps & { children…’ may not be a union type.

这是type definition for DragDropContext

export function DragDropContext<P>(
    backend: Backend
): <P>(componentClass: React.ComponentClass<P> | React.StatelessComponent<P>) => ContextComponentClass<P>;

我不能把它放在一起抱怨的错误是什么?似乎它不喜欢ComponentClass< P> | StatelessComponent< P>,但是那些不是元素属性,元素属性简单地是< P>.我试图明确地通过< P&gt ;::

export default DragDropContext<ExampleProps>(HTML5Backend)(ExampleScreen);

但同样的错误仍然存??在.我可以通过断言输出来解决它:

export default DragDropContext(HTML5Backend)(ExampleScreen) as React.ComponentClass<ExampleProps>;

但我不喜欢使用断言,我不明白实际的问题,或者我做错了事情.这是可以解决的打字问题吗?

您可以使用以下方式安装新的打字:
npm i @types/react-dnd --save-dev

如果您已经安装其他打字,请使用:

typings uninstall --save --global react-dnd

之后,它应该按预期工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读