'{ [x: string]: any; 型別的錯誤引數 }' 不能分配給'Contact' 型別的引數。輸入 '{ [x: string]: any; }' 缺少“聯系人”型別的以下屬性:id、contactType、name ts(2345)
const contact: {
[x: string]: any;
}
我有這個錯誤的問題。我正在嘗試在我的博覽會應用程式中下載聯系人。
我的代碼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);
}
}
uj5u.com熱心網友回復:
我認為您在型別推斷方面遇到了問題,您發送的聯系人物件缺少所需的引數,例如id, contactType, name, etc. 對于您當前沒有的屬性,您必須使用?運算子來標記它們可以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;
}
添加聯系人的方法應該類似于
class Contacts {
...
addContactAsync: (contact: IContact) => { ... }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/403673.html
標籤:
下一篇:反應原生導航v6身份驗證流程
