You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native:如何内联扩展函数参数的TS接口(避免单独定义)

解决React Native组件内联扩展TextProps的问题

你之前的写法错误在于用了extends关键字来合并类型,这在TypeScript的类型注解场景下不适用,正确的方式是用**交叉类型(&)**来合并自定义属性和原生TextProps,完全不用单独定义接口。

正确的内联写法如下:

const BarTabName = ({ active, ...props }: { active: boolean } & TextProps) => {
    return <Text {...props} />;
};

这个写法的效果:

  • 强制要求传入必填的active布尔属性
  • 同时兼容React Native Text组件的所有原生属性(比如style、numberOfLines等)
  • 完全满足你不想单独创建接口的需求

内容的提问来源于stack exchange,提问作者Grover

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 12:01:20