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"
这个配置必须和你的路由模式匹配,不能缺失。
- Pages Router模式下,确保
- 修改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
相关产品推荐
相关产品推荐

