Typescript - Undefined check in another function (Object is possibly undefined error)

Viewed 13

When i check undefined directly the ts works perfectly

const data: {result: number} | undefined = getApiData()

if(data.result === undefined){
    throw new Error('Data is undefined')
 }

data.result => Works perfectly

But when i want to achieve my undefined check in another function but i have a TS error

function checkUndefined(data: {result: number} | undefined ) {
 if(data.result === undefined){
    throw new Error('Data is undefined')
 }
}

const data: {result: number} | undefined = getApiData()

checkUndefined(data)

data.result => Object is possibly 'undefined'.ts(2532)

Do you have a solution cause i don't want to write the undefined check in all my function

0 Answers
Related