I am using data that is returned via a graphql query. Each of the user relations object looks like this:
"userRelations": [ {"relatedUser": {"id": 4,"firstName": "Jack","lastName": "Miller" },"type": "FRIEND" }, {"relatedUser": {"id": 3,"firstName": "Rhena","lastName": "Tahoma" },"type": "CONTACT" } ]
Currently, my code renders all relatedUsers
regardless of what their type
is. However, I only want to render those which have "type": "contact"
. I am trying to use the . filter
function but I am unable to do so.
type UserProps = { data: UsersLazyQueryHookResult;};type RelatedUser = { firstName: string, lastName: string, id: string, phoneNumber: string, }; type RelationType = { type: string, };export const ContactList: React.FunctionComponent<UserProps> = ({ data }) => { if (!data) return null; return (<View style={styles.users}> {data.users.nodes[0].userRelations.map( (item: { relatedUser: RelatedUser, type: RelationType}) => { // if (item.type: "CONTACT"){ return (<View style={styles.item} key={item.relatedUser.id}> ...</View></View> ); }, )}</View> );};
I tried using this: data.users.nodes[0].userRelations.filter(({ type }) => type === 'CONTACT').map(/*code*/)
but it gives me an error that:
Binding element 'type' implicitly has an 'any' type.ts(7031)
If I add : RelationType
infront of type, I get 'RelationType' is declared but its value is never read.ts(6133) Binding element 'RelationType' implicitly has an 'any' type
What else can I do to filter out and map/render only CONTACT types.