type A = { a: number, b: null } | { a: null, b: 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 }) // it comes error
})
How can I using like this situation?
CodePudding user response:
Passing the original object to ccc
works, as does spreading it into a new object.
ccc(temp)
ccc({ ...temp })
CodePudding user response:
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 })
})