TypeScript nicety. A variable's type is a union of classes:

declare const aorb: A | B

You want to handle each class separately with a sequence like

if (aorb instanceof A) { }
else if (aorb instanceof B) { }

You also want to be sure that if another class is added you don't forget to amend the if-cascade. Just add:

else { throw aorb satisfies never; }

Full code on playground. Can this be done with switch too?

https://www.typescriptlang.org/play/?#code/MYGwhgzhAEBCkFMA8AVaCAeAXBA7AJjPBMigHxnQDeAvgFCiQwCC62ehciSzlVd0QdABOCMPgD2uEAE9oYaAF5oAImYqA3HXqMocNjgJFusPgKGjxU2dABGS1bE3aG4PQGEDHYySTuzQiJiktJywA4q7s70dAD0sdDM+PjQujCeWBLyEsL2YATQJAjQABYSAO7QWCXFwBIAtgAOAJYgCMKpDY3gzbgwYLYSAK5YcQnVxb11TW040CDNEFjQEgBm0M2rABS9S-nACGsAlKmI-csT0AAGEGBYi6vNCBBXAHR0+AiMop19y2A5WwALkS0AAPnAtJtoFsAbkNn99od1swTvxAnU+hI2q8QBIAOZbFRw+wlSCqaAAamyuVeYCOWho6BAJA22xJCL2uAOazgaPMgkxEGxCFxBKJHLJMBUVJptletiO2mZrPRQmqwgqcsKdweTxguAQADd2oy6EA

#TypeScript #exhaustivenesstest #instanceof #exhaustive

TS Playground - An online editor for exploring TypeScript and JavaScript

The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.