我正在尝试使用泛型在typescript中创建以下函数,但它向我显示以下错误:
" Propoerty' id'在类型' CustomerInterface'"中不存在 这发生在: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
}
答案 0 :(得分:0)
一段时间后,我发现使用泛型约束= P的解决方案 应该是这样的:
getCustomer<T extends CustomerInterface>(id: number | string){
return this.getCustomersCloseTo<T>('')
.then(customers => customers.find(customer => customer.id === +id));
}