Argument of type '{ [x: string]: any; }' is not assignable to parameter of type 'Contact'

1.5k views Asked by At

Error Argument of type '{ [x: string]: any; }' is not assignable to parameter of type 'Contact'. Type '{ [x: string]: any; }' is missing the following properties from type 'Contact': id, contactType, name ts(2345)

const contact: {
    [x: string]: any;
}

I have a problem with this error. I'm trying to download a contact in my expo app.

My Code download.tsx:

const downloadContact = async (customer: any) => {
    if(hasContactPermission) {
        const contact = {
            [Contacts.Fields.Name]: customer.name,
            [Contacts.Fields.Emails]: customer.email,
            [Contacts.Fields.PhoneNumbers]: customer.phone,
        };
        const contactId = await Contacts.addContactAsync(contact);
    }
}
1

There are 1 answers

0
jbheber On BEST ANSWER

I think you are having an issue with the type inference, the contact object you are sending lacks on required parameters like id, contactType, name, etc. For properties that you don't currently have you have to use the ? operator to mark that they can be undefined

interface IContact: {
    id?: string;
    contactType?: string // <- using the ? marks them as optional parameters
    name: string // <- if the name is not provided the ts complier will complain
    [x: string]: any;
}

And the method to add contacts should be something like

class Contacts {
    ...
    addContactAsync: (contact: IContact) => { ... }
}