javascript - 错误 "Property ' type' is missing in type 'typeof WithXXX' "

标签 javascript reactjs typescript react-dnd

我将 React 与 Typescript 结合使用,并尝试基于 react-dnd 库创建高阶组件。错误发生在react-dnd组件的DragSource部分。这是相应的代码:

import React from 'react';
import { DragSource, ConnectDragSource, DragSourceConnector, DragSourceMonitor } from 'react-dnd';
import ItemTypes from './itemTypes'

const collect = (connect: DragSourceConnector, monitor: DragSourceMonitor) => {
    return {
        connectDragSource: connect.dragSource(),
        isDragging: monitor.isDragging()
    }
}

const templateObjectSource = {
    beginDrag(props: any) {
        return {}
    }
}

export interface TemplateObjectCollectedProps {
    canDrop: boolean
    isOver: boolean
    connectDragSource: ConnectDragSource
}

export interface TemplateObjectProps {
    name?: string
}

function withTemplateObjectDraggable(WrappedComponent: React.ComponentClass<TemplateObjectProps>) {
    return class WithTemplateObjectDraggable extends React.Component<TemplateObjectCollectedProps> {
        public render() {
            return this.props.connectDragSource(
                <div>
                    <WrappedComponent {...this.props} />
                </div>
            )
        }
    }
}

export default DragSource(ItemTypes.TEXTFIELD, templateObjectSource, collect)(withTemplateObjectDraggable)

Typescript 在最后一行给我以下错误,特别是在“(withTemplateObjectDraggable)”部分:

[ts]
Argument of type '(WrappedComponent: ComponentClass<TemplateObjectProps, any>) => typeof WithTemplateObjectDraggable' is not assignable to parameter of type 'ComponentType<ComponentClass<TemplateObjectProps, any>>'.
Type '(WrappedComponent: ComponentClass<TemplateObjectProps, any>) => typeof WithTemplateObjectDraggable' is not assignable to type 'FunctionComponent<ComponentClass<TemplateObjectProps, any>>'.
Type 'typeof WithTemplateObjectDraggable' is not assignable to type 'ReactElement<any>'.
Property 'type' is missing in type 'typeof WithTemplateObjectDraggable'. [2345]

我不确定我是否错误地使用了高阶组件,或者它是否只是一个类型主题。不幸的是,我没有找到将 react-dnd 与其他高阶组件结合起来的示例,所以我可能走错了路。如果你能给我提供一个链接到一个工作的 typescript (或 javascript)示例,它在 react-dnd 中的“DragSource”之上实现了一个高阶组件,它已经对我有帮助了,因为我找不到一个。

仅供引用:然后我会像这样使用高阶组件:

import React from 'react';
import imgTextfield from '../../../../../assets/templateObjects/textfield.png'
import withTemplateObjectDraggable from './withTemplateObjectDraggable'

class Textfield extends React.Component {
    public render() {
        return (
            <span>
                <img src={imgTextfield} />
                <div> Textfield </div>
            </span>
        )
    }
}

export default withTemplateObjectDraggable(Textfield)

最佳答案

看来你的逻辑有点不对。 DragSource 需要一个 React 组件作为输入,但您正试图将您的 withTemplateObjectDraggable HOC 传入。因此 Typescript 提示类型不兼容。

我不是 100% 确定它是否有效,但请尝试将 DragSource 移动到您的 HOC 中,如下所示:

function withTemplateObjectDraggable(WrappedComponent: React.ComponentClass<TemplateObjectProps>) {
    class WithTemplateObjectDraggable extends React.Component<TemplateObjectCollectedProps> {
        public render() {
            return this.props.connectDragSource(
                <div>
                    <WrappedComponent {...this.props} />
                </div>,
            );
        }
    }

    return DragSource(ItemTypes.TEXTFIELD, templateObjectSource, collect)(WithTemplateObjectDraggable);
}

关于javascript - 错误 "Property ' type' is missing in type 'typeof WithXXX' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53807202/

相关文章:

javascript - 在 useImperativeHandle 内部调用的调度不调用操作

reactjs - 如何将 react-router-dom 升级到 v6

reactjs - 如何使用类型化上下文提供程序创建通用 React 组件?

javascript - 如何以reactjs形式绑定(bind)数据

javascript - Hapi/Joi 验证

javascript - TypeScript - "debugger;"语句在 Angular 中使用 QA 环境时不起作用

javascript - jQuery 点击 td 切换复选框

javascript - 如何在运行时更改标签值?

JavaScript 函数参数 - 语法错误

javascript - 插值/绑定(bind)中的 Angular 、数学函数