-
Notifications
You must be signed in to change notification settings - Fork 105
Description
Introduction
Most statically typed languages do not provide first class support for generic union types.
Besides missing language support, unions are often hard to disambiguate during deserialization.
ESLint rules to implement
1. Prefer tagged variants
Condition: Use of union A | B | ...
for a field/property when all members are class
types.
Severity: 🔴 error
Message: Use tagged variants instead (external
or internal
).
Exceptions: Exceptions only allowed for legacy APIs. Never suppress for new APIs.
Use internal
tagged variants to model a form of polymorphism that is as well broadly supported by JSON serializer libraries.
Use external
tagged variants (containers), if the individual union variants are not directly related.
2. Do not use inline unions
Condition: Use of union A | B | ...
for a field/property.
Severity: 🔴 error
Message: Use an alias type instead.
Exceptions: None.
Defining an alias type export type MyUnion = A | B | ...
and using this as the field/property type instead of the inline union, allows to hand-craft the type in some statically typed languages which greatly improves usability or even is the only way to correctly some union types at all.