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