【发布时间】:2017-09-05 21:37:33
【问题描述】:
我正在使用 TypeScript (2.4.2) 首次涉足 React (15.6.1),并且我正在尝试创建一个表示可拖动的 JSON 字段的组件。这是我的组件代码:
import * as React from "react";
interface JsonFieldProps {
name: string;
type: string;
indent: number;
}
export class JsonField extends React.Component<JsonFieldProps, undefined> {
marginStyle = {
'text-indent': `${this.props.indent * 15}px`
};
render() {
return <div draggable={true} onDragStart={handleDrag} style={this.marginStyle}>{this.props.name}: {this.props.type},</div>;
}
}
function handleDrag(ev: DragEvent): void {
let id = (ev.target as HTMLDivElement).id;
ev.dataTransfer.setData("text/plain", id);
}
当我尝试编译它(使用 webpack)时,我收到以下错误:
ERROR in [at-loader] ./src/components/JsonField.tsx:15:21
TS2322: Type '{ draggable: true; onDragStart: (ev: DragEvent) => void;
style: { 'text-indent': string; }; child...' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'.
Type '{ draggable: true; onDragStart: (ev: DragEvent) => void; style: { 'text-indent': string; }; child...' is not assignable to type 'HTMLAttributes<HTMLDivElement>'.
Types of property 'onDragStart' are incompatible.
Type '(ev: DragEvent) => void' is not assignable to type 'EventHandler<DragEvent<HTMLDivElement>>'.
Types of parameters 'ev' and 'event' are incompatible.
Type 'DragEvent<HTMLDivElement>' is not assignable to type 'DragEvent'.
Property 'initDragEvent' is missing in type 'DragEvent<HTMLDivElement>'.
我不确定我是否使用了错误的事件类型,但如果是错误的,我似乎找不到任何关于哪个是正确的信息。
【问题讨论】:
标签: javascript reactjs typescript