使用react-native-elements图标遇TypeScript错误,如何解决?
问题说明
使用react-native-elements的图标组件时触发TypeScript类型检查错误,错误提示:
Property 'tvParallaxProperties' is missing in type '{ type: string; name: string; color: string; size: number; }' but required in type 'Pick<IconButtonProps & { type?: string | undefined; Component?: typeof Component | undefined; reverse?: boolean | undefined; raised?: boolean | undefined; containerStyle?: StyleProp<...>; ... 5 more ...; brand?: boolean | undefined; } & Partial<...>, "backgroundColor" | ... 68 more ... | "brand">'.
当前使用的依赖版本:
react-native: ^0.64.1react-native-elements: ^3.4.1
修复方案
显式传入tvParallaxProperties属性
在图标组件的props中添加tvParallaxProperties,直接设为undefined即可满足类型要求:<Icon type="material" name="home" color="#fff" size={24} tvParallaxProperties={undefined} />使用类型断言跳过检查
若不想额外添加属性,可通过类型断言让TypeScript忽略该属性的缺失:<Icon type="material" name="home" color="#fff" size={24} /> as React.ReactElement升级依赖版本(推荐)
该错误源于react-native-elements的类型定义与react-native版本不兼容,升级react-native-elements至更高版本(如>=3.4.2),官方通常会在后续版本修复这类类型匹配问题。
内容的提问来源于stack exchange,提问作者gnanasai Dachiraju

