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

NextJS+TypeScript提交时tsc报错:JSX未启用及SVG模块找不到

解决NextJS + TypeScript在lint-staged执行tsc --noEmit时的两类错误

错误1:TS17004:Cannot use JSX unless the '--jsx' flag is provided

  • 检查tsconfig.json的compilerOptions配置:
    • Pages Router模式下,确保"jsx": "react-jsx"
    • App Router(NextJS 13+)模式下,确保"jsx": "preserve"
      这个配置必须和你的路由模式匹配,不能缺失。
  • 修改lint-staged的命令,明确指定tsconfig路径:
    把"tsc --noEmit"改成"tsc --noEmit --project ./tsconfig.json",避免tsc自动查找配置文件时出错。如果是monorepo,把路径换成子项目对应的tsconfig路径即可。

错误2:TS2307:Cannot find module '@/icons/navbar/logout.svg' or its corresponding type declarations

  • 新增SVG类型声明文件:
    在项目根目录创建types/svg.d.ts,写入以下代码:
    declare module '*.svg' {
      import type { ReactElement, SVGProps } from 'react';
      const content: (props: SVGProps<SVGElement>) => ReactElement;
      export default content;
    }
    
  • 让tsconfig识别这个类型文件:
    修改tsconfig.json的include数组,加入类型文件路径:
    "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "types/**/*.d.ts"]
    
  • 验证@别名的路径映射:
    确认tsconfig.json的compilerOptions.paths配置正确:
    "@/*": ["./*"]
    
    保证tsc能解析@/开头的路径。

额外提示:如果问题仍存在,建议在package.json里新增脚本"tsc:check": "tsc --noEmit",然后让lint-staged调用这个脚本(比如"tsc:check"),确保使用项目本地安装的TypeScript版本,避免全局版本和项目版本不一致导致的兼容问题。

内容的提问来源于stack exchange,提问作者MD SOHEL

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 22:29:53