精彩文章免费看

类型“EventTarget”上不存在属性“classList”。ts(2339)

报错代码

  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属性.