簡體   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