简体   繁体   English

在 function 上扩展内联 typescript 接口

[英]extend inline typescript interface on function

I have a pretty simple React component which takes a required prop, active .我有一个非常简单的React组件,它需要一个必需的道具active

Setting the inline interface for this one prop is quite simple.为这个道具设置内联接口非常简单。 But now, I need to account for the other types of props that can be passed onto the component.但是现在,我需要考虑可以传递到组件的其他类型的道具。

Luckily, Native supplies the props for this element, so I don't have to recreate the interface for it.幸运的是, Native为这个元素提供了 props,所以我不必为它重新创建界面。 But I'm struggling to figure out how to extend my current inline interface to get it to work.但我正在努力弄清楚如何扩展我当前的内联接口以使其正常工作。

I've tried this approach:我试过这种方法:

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

But this just results in the error:但这只会导致错误:

'?' “?” expected.ts(1005)预期.ts(1005)

If possible, I'm trying to avoid creating a separate interface outside of the parameters.如果可能的话,我试图避免在参数之外创建一个单独的接口。

Use an intersection:使用交集:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM