Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: How to add type guards for enums in union types?

The following code gives errors:

  1. The left-hand side of an 'instanceof' expression must be of type 'any', an object or a type parameter.
  2. Type 'E | C' is not assignable to type 'E'. Type 'C' is not assignable to type 'E'.
    enum E { FIRST = 1, SECOND = 2 }; 

    class C {
        value: E;    

        constructor(arg: C | E) {
            if (arg instanceof C) { // 1.
                this.value = arg.value;
            } else {
               this.value = arg; // 2.
            }
        }
    }

    var a: C = new C(E.SECOND);
    console.log('a.value = ' + a.value);

    var b: C = new C(a);
    console.log('b.value = ' + b.value);

Despite the errors the code seems to compile fine on TypeScript Playground and does the expected.

like image 345
Grathau Avatar asked Jan 09 '23 13:01

Grathau


1 Answers

Two issues.

There's a bug in the compiler that instanceof isn't allowed on union types when one of its constituents isn't an object type. This is being fixed (https://github.com/Microsoft/TypeScript/issues/2775)

The other thing is that instanceof does not cause narrowing in the else block (https://github.com/Microsoft/TypeScript/issues/1719) because failing an instanceof check doesn't necessarily mean the object doesn't match the specified interface. This is currently "by design" but I'd encourage you to leave a comment that you find this behavior surprising or undesirable (I certainly do).

like image 51
Ryan Cavanaugh Avatar answered Jan 15 '23 20:01

Ryan Cavanaugh