Deep in Software with Hugo

Subscribe
Archives
September 2, 2025

Advanced TypeScript #3: Include/Exclude from union

Here’s part 3 of 7 of the Advanced TypeScript series, where I share TypeScript patterns I’ve encountered in enterprise grade applications.

3. Include/Exclude TypeScript type(s) from Union

Include/Exclude can help remove members from a TypeScript union.

import { expectTypeOf } from 'expect-type';
type ValueType1 = { myField: string };
type ValueType2 = {
  myOtherField: string;
};
type ValueType3 = { myField2: string };
type PossibleValues = ValueType1 | ValueType2 | ValueType3;

expectTypeOf<Exclude<PossibleValues, ValueType1>>().toEqualTypeOf<
  ValueType2 | ValueType3
>();

This pattern is useful when some functionality is only relevant to a subset or superset of the types in the union.

That's this week's pattern, you can get a sneak peek of the rest at codewithhugo.com/typescript-types-in-the-trenches/, or access the annotated source at github.com/HugoDF/real-world-ts.

Don't miss what's next. Subscribe to Deep in Software with Hugo:
GitHub Bluesky
Powered by Buttondown, the easiest way to start and grow your newsletter.