Nadiely Jade
Nadiely Jade

Reputation: 255

Union type in typescript to solve incompatibly

I create a small example below to address my issue:

interface testType {
    id: number
}

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

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

assume that testType came from somewhere else which I don't have control. I want to add a new array of object which its id type is not number. I don't want to make a new variable name but to use the t

Upvotes: 0

Views: 45

Answers (1)

Łukasz Karczewski
Łukasz Karczewski

Reputation: 1218

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'
}))

Upvotes: 1

Related Questions