簡體   English   中英

typescript中的聯合類型解決不兼容

[英]Union type in typescript to solve incompatibly

我在下面創建了一個小示例來解決我的問題:

interface testType {
    id: number
}

let t: testType[] = [{
    id: 1
}]

t = t.map(item => ({
    ...item,
    id: '123'
}))

假設testType來自我無法控制的其他地方。 我想添加一個新的 object 數組,它的id類型不是number 我不想創建一個新的變量名,而是使用t

interface testType {
    id: number
}

type unionIdTestType = Omit<testType, "id"> & {id: string | testType["id"]}

let t: unionIdTestType[] = [{
    id: 1
}]

t = t.map(item => ({
    ...item,
    id: '123'
}))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM