![](/img/trans.png)
[英]Can't seem to create an interface to satisfy TypeScript getting missing property
[英]Generics function in typescript can't find interface property
我正在嘗試使用泛型在打字稿中創建以下函數,但它向我顯示以下錯誤:
“類型'CustomerInterface'上不存在屬性'id'”發生在:customer.id === + id
getCustomer<CustomerInterface>(id: number | string){
return this.getCustomers<CustomerInterface>('')
.then(customers => customers.find(customer => customer.id === +id));
}
接口定義:
export interface CustomerInterface {
id: number
name: string
display_name: string
address: string
city: string
phone_number: string
}
一段時間后,我發現使用泛型約束= P的解決方案應該是這樣的:
getCustomer<T extends CustomerInterface>(id: number | string){
return this.getCustomersCloseTo<T>('')
.then(customers => customers.find(customer => customer.id === +id));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.