The more lines of code you write, the more maintenance is required, and the more code you have to change. In the example above, the kind field is the discriminator and has a value of "AUTHENTICATED" Each union member can contain fields that are only relevant to its specific kind.

9m read timeFrom blog.logrocket.com
Post cover image
Table of contents
Flaws with using a single-interface approachOver 200k developers use LogRocket to create better digital experiencesUsing algebraic data types and impossible statesDetermining if union members are current (a.k.a., type narrowing)Using ts-pattern instead of switch statementsParse, don’t validate: Using a type-safe validation schemaConclusion: Tests and types are not mutually exclusiveLogRocket : Full visibility into your web and mobile apps

Sort: