React自定义组件传props报IntrinsicAttributes相关TS错误如何解决?
问题原因与修复方案
类型报错根因
你定义FadeInSection组件时,没有给FunctionComponent传入对应的Props泛型参数,导致TypeScript无法识别该组件支持传入offset属性,因此触发类型报错。
原错误写法:
export const FadeInSection: FunctionComponent = ({ offset, children }: FadeInSectionProps) => {
修复写法:
// 将Props类型传入FunctionComponent泛型,参数自动继承类型无需重复注解 export const FadeInSection: FunctionComponent<FadeInSectionProps> = ({ offset, children }) => {
可选优化:避免自定义样式属性丢失
你使用的@styled-system/should-forward-prop默认仅转发HTML标准属性和styled-system内置属性,虽然当前功能运行正常,但为了避免后续迭代出现样式失效问题,建议将自定义的offset属性加入转发白名单:
const Section = styled.div.withConfig({ shouldForwardProp: (prop) => prop === 'offset' || shouldForwardProp(prop), })<{ offset?: number }>` // 原有样式逻辑保持不变 `;
Input相关报错处理
你贴出的报错信息属于Input组件的类型错误,和当前的FadeInSection组件无关联:
请检查你自行封装的Input组件的InputProps接口定义,补充type?: string字段即可消除该报错。
内容的提问来源于stack exchange,提问作者Toni Michel Caubet
相关产品推荐
相关产品推荐

