You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用React.MouseEvent<HTMLElement>遇报错:仅TypeScript文件可用类型注解

解决React.MouseEvent类型注解报错:Type annotations can only be used in TypeScript files.ts(8010)

这个错误的根源是你在**JavaScript文件(.js/.jsx)**中使用了TypeScript专属的类型注解语法,JavaScript本身不支持这种类型标注,因此编译器抛出该错误。

解决办法:

  1. 切换到TypeScript文件
    将当前文件的后缀从.jsx改为.tsx(React项目中TypeScript文件的标准后缀),这样TypeScript编译器就能正确识别并处理React.MouseEvent<HTMLElement>这类类型注解。

    确保你的React项目已配置TypeScript支持:

    • 若用create-react-app创建,需在初始化时选择TypeScript模板;
    • 手动配置的话,需安装typescript、@types/react、@types/react-dom依赖。
  2. 移除类型注解(保持使用JavaScript)
    如果不想迁移到TypeScript,直接删掉参数的类型标注即可,JavaScript会自动推断参数类型:

    const handleOpenNavMenu = (event) => {
        setAnchorElNav(event.currentTarget);
    };
    
    const handleOpenUserMenu = (event) => {
        setAnchorElUser(event.currentTarget);
    };
    

内容的提问来源于stack exchange,提问作者Abdul Sami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 03:29:57