I haven’t use Typescript in many years and can’t remember nor find how to type guard many classes inside a switch statement correctly.
class A {}
class B {}
class C {}
type OneOfThem = A | B | C;
function test(foo: OneOfThem): string {
switch(/* something using foo */) {
/* A */:
return "A";
/* B */:
return "B";
/* C */:
return "C";
/* should not need to use "default" as all cases are handled */
}
}
I found and tried several options like:
- Using
foo.constructor
- Using
instanceof
inside the case statements - adding an extra member to the three classes to be used in the switch statement
but none of them works (Function lacks ending return statement and return type does not include 'undefined'
).
Is my memory playing tricks and this is not possible with classes ?