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

Get a type as enum values of object and pass the readonly object as props - React Typescript

$
0
0

I have the following data:

const ActionData = [  { label: 'Edit', icon: 'pen', key: 'edit' },  { label: 'Skip', icon: 'forward', key: 'skip' },  { label: 'Stop', icon: 'stop-circle', key: 'stop' },];

which I use to display list of Actions

{ActionData.map((dataItem) => (<ActionItem           key={dataItem.key}          item={dataItem}           onPress={handleActionClick}         />))}

I need a type ActionItemKeyType which should be 'edit' | 'skip' | 'stop'. i.e. keys of ActionData

If I declare ActionData as const

const ActionData = [  { label: 'Edit', icon: 'pen', key: 'edit' },  { label: 'Skip', icon: 'forward', key: 'skip' },  { label: 'Stop', icon: 'stop-circle', key: 'stop' },] as const;

then I am able to get the type ActionItemKeyType as expected using the following code

type ActionItemKeyType = typeof ActionData[number]['key'];

But if I declare ActionData in this way, then I can't pass the item props in my render method because the Array is read-only.

How can I achieve both the functionalities?


Viewing all articles
Browse latest Browse all 6295

Trending Articles



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