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

https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UDeUCGAuKA7ArgWwCMIAnAGigM1wBtqoBfKAH2TSp1vMu3yOIYDcAKCEBjAPZYAzsDSoMsANoBdBFEUoFARi7taDcpr3VdULQ2XCRAMxxZRwAJaSootwAowxcWCmYYAJTIIvQi8qgAdHioYO7AEHhgCAB8wVDprh5GUPGJEaimuWARBAwBQvQBQA

How can I using like this situation?

2 Answers 2

1

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

ccc(temp)
ccc({ ...temp })
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Sorry for not enough information. I want to when 'a' number 'b' must to be null and inverse situation. but thank you for answering me!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.