dimanche 26 juillet 2020

Best way to check non null precondition in typescript

Imagine a firebase timestamp that you want to check for equality like so:

if (!a.timestamp.isEqual(b.timestamp){
   //Do stuff because timestamp of a and b are not equal
}

This obviously fails if a is null or if timestamp of a is null (Does this also fail if timestamp of b is null?). Lets assume a and b are not null. What is the best way to ensure that the above statement works. This seems way to complicated:

if (a.timestamp === null && b.timestamp !== null ||
    a.timestamp !== null && b.timestamp === null ||  
    a.timestamp !== null && !a.timestamp.isEqual(b.timestamp))
{
        //Do stuff because timestamp of a and b are not equal
}

Aucun commentaire:

Enregistrer un commentaire