let entities: {
clickPrice: number
} | undefined = Math.random() >= 0.5 ? {
clickPrice: Math.random()
} : undefined
const clickPrice = Math.random() >= 0.5 ? Math.random() : undefined
type testType = {
clickPrice: number
}
const test: {
clickPrice: number
}[] = []
if (entities || clickPrice) {
test.push({
clickPrice: entities ? entities.clickPrice : clickPrice
})
}
Why do I get this error:
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'.
on clickPrice
clickPrice: entities ? entities.clickPrice : clickPrice
?
What I want is "If there's entities, or clickPrice, execute some code, which could use only one of them".
TS playground