报错代码
const onConnectEnd = useCallback(
(event: React.MouseEvent<HTMLDivElement, MouseEvent> ) => {
const targetIsPane = event.target.classList.contains('react-flow__pane')
},
[project],
)
如图
方案
const onConnectEnd = useCallback(
(event: React.MouseEvent<HTMLDivElement, MouseEvent> ) => {
const target = event.target as HTMLDivElement // 需要明确告诉TypeScript 目标HTMLElement的类型.
const targetIsPane = target.classList.contains('react-flow__pane')
},
[project],
)
这将让TypeScript知道元素是一个 div 并且它将知道classList属性.