4 ms·
Coming from TypeScript, having an explicit discriminator field (like "type") is a lot easier to deal with[1] (which to be fair is compatible with the last examp
by agency 4y ago
Coming from TypeScript, having an explicit discriminator field (like "type") is a lot easier to deal with[1] (which to be fair is compatible with the last example given in the OP where they have a "type" field and a generic "data" payload field).
[1] https://www.typescriptlang.org/docs/handbook/2/narrowing.html#discriminated-unions https://www.typescriptlang.org/docs/handbook/2/narrowing.htm...
- TheCoelacanth 4y agoI don't think it's any easier. Narrowing works the same way with the in operator. If A, B and C each have a single property with the same name: function f(x: A | B | C) { if ('A' in x) { // x is narrowed to A } else { // x is narrowed to B | C } }