简体   繁体   English

TypeScript中的泛型函数找不到接口属性

[英]Generics function in typescript can't find interface property

I am trying to create the following function in typescript using generics but it is showing me the following error: 我正在尝试使用泛型在打字稿中创建以下函数,但它向我显示以下错误:

"Propoerty 'id' does not exist on type 'CustomerInterface'" This happens in: customer.id === +id “类型'CustomerInterface'上不存在属性'id'”发生在:customer.id === + id

        getCustomer<CustomerInterface>(id: number | string){
            return this.getCustomers<CustomerInterface>('')
                .then(customers => customers.find(customer => customer.id === +id));
        }

The interface definition: 接口定义:

export interface CustomerInterface {
    id: number
    name: string
    display_name: string
    address: string
    city: string
    phone_number: string
}

A time later I found the solution using Generics constraints =P It should be like: 一段时间后,我发现使用泛型约束= 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM