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

How to check instance of parameters in function?

$
0
0

I have to different interface

interface Vehicle {// data about vehicle}interface Package {// data about package}

I have a component that in it's props can receive either of them (and maybe more in the future),so I created props interface as such:

interface Props<T> {  modalVisible: boolean,  selectableData: T[],  currentlySelectedIndex: number,  onSelect: (selection: T) => void,  onBackPressed: () => void}

In my component I have a FlatList that receives the selectableData and a renderItem function that like this:

<FlatList      data={selectableData}      renderItem={renderItem}      style={{ maxHeight: 251 }}      ItemSeparatorComponent={() => <FlatListItemSeparator separatorStyle=       {styles.itemSeparator} />}      keyExtractor={(item, index) => index.toString()} />  const renderItem: ListRenderItem<Vehicle | Package> = ({ item }) => (<ModalSelectionItem      topText={ item instance Vehicle ? 'a' : 'b'}    />  );

I want to render different text in the FlatList based on type that was passed, how can I check what type of I have? I tried to use both instanceof and typeof but none of them seems to work


Viewing all articles
Browse latest Browse all 6287

Trending Articles



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