繁体   English   中英

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

[英]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.

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