已配置resolutions仍出现TouchableOpacity无法作为JSX组件的错误
问题原因分析
报错信息翻译
'TouchableOpacity' cannot be used as a JSX component.其实例类型TouchableOpacity不是有效的JSX元素。render()返回的类型不兼容,类型import("/Users/sanjeevkumar/React/help_idea_update/mypatapp-v2-update/node_modules/@types/react-native/node_modules/@types/react/index").ReactNode无法赋值给类型React.ReactNode。
可能的原因
- @types/react 版本冲突未彻底修复:虽然配置了resolutions,但嵌套依赖中的@types/react可能没被强制替换。执行
yarn list @types/react查看依赖树,确认是否存在多个版本的@types/react共存。 - 缓存残留导致配置未生效:yarn缓存或TypeScript缓存保留了旧类型信息。需要删除
node_modules、yarn.lock文件,执行yarn cache clean清理缓存后重新yarn install;同时删除tsconfig.tsbuildinfo(若存在),重启IDE。 - react-native 与 @types/react 版本不兼容:你指定的@types/react 17.0.2可能和当前react-native版本不匹配。需确认react-native官方文档中对应的@types/react兼容版本,调整resolutions中的版本号。
- IDE 类型识别未更新:VS Code等IDE可能未及时加载新的类型定义,可通过重启IDE或触发TypeScript服务重启(VS Code中按
Ctrl+Shift+P选择TypeScript: Restart TS Server)解决。
内容的提问来源于stack exchange,提问作者Sanjeev Kumar
相关产品推荐
相关产品推荐

