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

How do I narrow the following type?

$
0
0

I have the following typescript type and function:

type ArgumentComponentItemType = React.ComponentType<{ props: { id: string }}>;type ArgumentComponentType =   | ArgumentComponentItemType  | ArgumentComponentItemType[];type ArgumentStringType =  | string  | string[];type ArgumentType =   | ArgumentComponentType  | ArgumentStringType;

and the function:

function format(id: string, args: ArgumentType): string {  const params = Array.isArray(args) ? args : [args];  const values = params.map(param => {    // The first `if` is the troublesome one.    if (param as FormatArgComponentItemType) {      return resolver((param as FormatArgComponentItemType).props.id);    } else if (typeof param === 'string') {      return param;    }    return '';  });  return values.join('');}

However the compiler won't budge, and I'm a bit at loss of how to actually properly access the id property of the React Component item giving me the following error:

Property 'props' does not exist on type 'FormatArgComponentItemType'.  Property 'props' does not exist on type 'ComponentClass<{ props: { id: string; }; }, any>'

I've looked at type guards using predicates but I think I have the same issue:

function isFormatArgComponentItemType(  value: string | FormatArgComponentItemType): value is FormatArgComponentItemType {  return (value as FormatArgComponentItemType).props.id !== undefined;}

Any help is appreciated!


Viewing all articles
Browse latest Browse all 6291

Trending Articles



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