#defacto way to check union types and do things based on result

6 messages · Page 1 of 1 (latest)

teal egret
#

I have a function with the following signature

static new(bucket: Bucket, key: string): Cas | Error {

I want to call it and then do things based on whether the return type is Cas or Error. Is this possible?

#

Cas is a class and Error is another union type

wicked minnow
#

!hb narrowing

strong beaconBOT
slim wyvern
#
const result = example.new(bucket, key);
if(result instanceof Cas) {
    // do stuff with `result: Cas`
}
else {
    // do stuff with `result: Error`
}
teal egret
#

thank you