Expo项目集成TypeScript报错:已装依赖仍提示缺少@types/react
解决Expo项目中TypeScript依赖检测异常问题
具体排查与修复步骤
- 检查
tsconfig.json配置
确保compilerOptions里没有错误屏蔽@types/react的配置,比如不要在types字段里遗漏react,或者直接移除types字段(默认会自动加载所有@types包)。参考标准配置片段:{ "compilerOptions": { "target": "esnext", "module": "commonjs", "lib": ["es2017"], "allowJs": true, "jsx": "react-native", "noEmit": true, "isolatedModules": true, "strict": true, "moduleResolution": "node", "allowSyntheticDefaultImports": true, "esModuleInterop": true, "skipLibCheck": true, "resolveJsonModule": true }, "exclude": ["node_modules", "babel.config.js", "metro.config.js", "jest.config.js"] } - 校验依赖实际安装版本
执行yarn list @types/react查看本地实际安装的版本,确认是否和package.json中声明的~17.0.21一致。如果版本不匹配,强制重新安装指定版本:yarn add --dev --force @types/react@~17.0.21 - 清除Expo缓存
Expo可能缓存了旧的依赖状态,执行命令彻底清除:expo start --clear - 清理Yarn缓存并重装依赖
若以上步骤无效,清理Yarn本地缓存后重新安装所有依赖:yarn cache clean rm -rf node_modules yarn.lock yarn install - 确认React核心包版本兼容性
@types/react版本必须和项目中react核心包版本匹配,比如@types/react@17.x对应react@17.x。执行yarn list react查看版本,若不匹配,调整react版本到兼容范围:yarn add react@~17.0.2
内容的提问来源于stack exchange,提问作者RossB
相关产品推荐
相关产品推荐

