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

extract object from array - typescript

$
0
0

I have a trip object that looks like this:

Array (1)0 {id: 1, vehicle: {freeSeats: 2, __typename: "Vehicle"}, startLocation: "{\"type\":\"Point\",\"coordinates\":[8.217462,53.13975]}", endLocation: "{\"type\":\"Point\",\"coordinates\":[8.258844,53.119525]}", timeOfDeparture: "2020-06-16T11:48:00.869Z", …}
type TripListProps = {  trips: any; //TODO FIX  //trips: Array<Trip>,  friendIds: Array<number>,};export const TripList: React.FunctionComponent<TripListProps> = ({ trips, friendIds }) => {  console.log('trips', trips);  if (trips.length > 0) {    return ( <View style={{ height: 500 }}><FlatList          data={trips}          horizontal={false}          scrollEnabled          renderItem={({ item }) => <TripContainer trip={item} friendIds={friendIds} />}          keyExtractor={(trip: any) => trip.id.toString()}        /></View>    )  } else {    return (<View style={styles.noTripsFound}><Text style={styles.text}>No trips found</Text></View>);  }};

It's original type is Array<Trip>. However, here I am passing it to another component TripContainer, which requires trip to be in this form:

trip: {  driver: {      firstName: string;      rating: number;      id: number;  };  timeOfDeparture: any;}

Because of this, if I change my TripListPropsfrom trips: any to trips: Array<Trip>, I get an error.

Is there any way I can extract only this part from the whole array object?


Viewing all articles
Browse latest Browse all 6213

Trending Articles



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