0

I have this piece of code where I check if hostel.country.address is null but nevertheless

return hostel.country.address &&
               hostel.country.address.internalEmployeeIdentifier !== null ||
               hostel.country.address.externalEmployeeIdentifier !== null;

I have this compilation problem on

hostel.country.address



Object is possibly 'null' or 'undefined'.


- error TS2533: Object is possibly 'null' or 'undefined'.
Sandro Rey
  • 2,429
  • 13
  • 36
  • 80
  • Try this: return hostel && hostel.country && hostel.country.address && hostel.country.address.internalEmployeeIdentifier !== null || hostel.country.address.externalEmployeeIdentifier !== null; – Lazar Nikolic Jan 14 '20 at 10:05
  • The compiler tells you exactly what you need to look into: that there are cases where `hostel.country.address` may be null or undefined. Is that expected? – Terry Jan 14 '20 at 10:05
  • Does this answer your question? [Javascript test ( object && object !== "null" && object !== "undefined" )](https://stackoverflow.com/questions/12535403/javascript-test-object-object-null-object-undefined) – Ray Jan 14 '20 at 10:11

6 Answers6

1
return hostel.country.address &&
               hostel.country.address!.internalEmployeeIdentifier !== null ||
               hostel.country.address!.externalEmployeeIdentifier !== null;

should work.

Good luck

Benoit Messiaen
  • 224
  • 1
  • 5
1

Please try this

const address = hostel?.country?.address
return address?.internalEmployeeIdentifier !== null || address?.externalEmployeeIdentifier !== null
TopW3
  • 1,477
  • 1
  • 8
  • 14
0

Add something like this, then modify your return line to use it:

function isValid(test) {
    return !(test === null || test === undefined);
}

Your return could look like:

return isValid(hostel) && 
       isValid(hostel.country) && 
       isValid(hostel.country.address) &&
       (isValid(hostel.country.address.internalEmployeeIdentifier) ||
        isValid(hostel.country.address.externalEmployeeIdentifier));
SPlatten
  • 5,334
  • 11
  • 57
  • 128
0

When you have nested properties and parent property may or may not exist, it is good to take help of some external library. something like this can make it much simpler

const _ = require('lodash');
if(_.get(hostel, 'country.address.externalEmployeeIdentifier')) {
  // do something
}

this way you do not need multiple && conditions. The library will take care of it.

Ashish Modi
  • 7,529
  • 2
  • 20
  • 35
0

One more way is to use object destructuring with default values.

  const {
    country: {
      address: { internalEmployeeIdentifier, externalEmployeeIdentifier } = {}
    } = {}
  } = hostel || {};

  return (
    internalEmployeeIdentifier !== null || externalEmployeeIdentifier !== null
  );
Siva K V
  • 10,561
  • 2
  • 16
  • 29
0

You can use optional chaining (you should install the babel plug in) and then your code will be something like:

hostel?.country?.address

more information can be found at:

https://github.com/tc39/proposal-optional-chaining

https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining

installation: https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining#installation

settings .babelrc

{
  "plugins": ["@babel/plugin-proposal-optional-chaining"]
}