typescript mapping type does not fit

Viewed 29
2 Answers

Passing the original object to ccc works, as does spreading it into a new object.

ccc(temp)
ccc({ ...temp })

Using Unions Types properly can rectify this error, change your type A

from

type A = { a: number, b: null } | { a: null, b: number };

to

type A = { a: number | null, b: null | number };

type A = { a: number | null, b: null | number };

const aaa: A[] = [{ a: 1, b: null }, { a: null, b: 1 }];

function ccc(props: A) {

}

aaa.map(temp => {
    ccc({ a: temp.a, b: temp.b })
})
Related