Reputation: 263
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?
Upvotes: 1
Views: 43
Reputation: 429
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 })
})
Upvotes: 0
Reputation: 371233
Passing the original object to ccc
works, as does spreading it into a new object.
ccc(temp)
ccc({ ...temp })
Upvotes: 1