React Native(TypeScript)中传递展开Props时报错问题求助
解决React Native Expo + TypeScript中展开TextInputProps的类型错误
以下是针对你遇到的类型报错的具体解决方案:
1. 确认组件与Props的导入来源
确保TextInput和TextInputProps都从react-native包导入,而非react或react-dom(Web端组件的类型与React Native完全不匹配,会直接导致该报错):
// 正确导入方式 import { TextInput, TextInputProps } from 'react-native'; // 错误示例(需避免) // import { TextInput } from 'react-dom';
2. 对齐@types/react与React Native的版本
Expo项目中React的版本由SDK固定,需确保@types/react版本与项目依赖的react版本严格匹配:
- 执行命令查看当前项目的React版本:
npm ls react
- 安装对应版本的类型包(例如当前React版本为
18.2.0,则执行):
npm install @types/react@18.2.0 --save-dev
3. 优化TypeScript配置(可选)
在tsconfig.json的compilerOptions中添加isolatedModules: true,这是Expo TypeScript项目的官方推荐配置:
{ "extends": "expo/tsconfig.base", "compilerOptions": { "strict": true, "isolatedModules": true } }
4. 清除TypeScript缓存
TS缓存可能导致错误的类型推断,可通过以下方式清理:
- 删除项目根目录下的
node_modules/.cache文件夹 - 在VSCode中按
Ctrl+Shift+P,选择TypeScript: Restart TS Server重启语言服务
内容的提问来源于stack exchange,提问作者SnowSuno
相关产品推荐
相关产品推荐

