Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6208

How to access React child element props in a type safe manner with TypeScript?

$
0
0

I'm trying to access props of a React [Native] component in my app (which are guaranteed to be instance of element of my custom type ItemTemplate:

const children = React.Children.toArray(this.props.children);
return children.find(t => t.props.itemKey == key);

However, in the second line when I try to access t.props I'm getting:

Property 'props' does not exist on type 'ReactElement<ItemTemplate, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)> | ... 13 more ... | (ReactElement<...>[] & ReactPortal)'.
  Property 'props' does not exist on type 'ReactElement<ItemTemplate, string | ((props: any) => ReactElement<any, string | ... | (new (props: any) => Component<any, any, any>)> | null) | (new (props: any) => Component<any, any, any>)>[] & string'.

The code does work correctly, but TypeScript (3.6.3) IntelliSense is complaining. Why?


Viewing all articles
Browse latest Browse all 6208

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>