# exhaustive matching

Exhaustive matching is the requirement that pattern matching include a branch capable of matching every possible case, as used in F# expressions.

This is one page of public article previews, not the complete archive. Follow Next page to continue. Summaries are not the original full articles.

## Nice and Naughty Cases of Pattern Matching

DevFeed: [Nice and Naughty Cases of Pattern Matching](<https://devfeed.tech/articles/nice-and-naughty-cases-of-pattern-matching-23025.md>)

Original publisher: [Read original article](<https://www.javaadvent.com/2025/12/nice-and-naughty-cases-of-pattern-matching.html>)

Author: Cay Horstmann

Published: 2025-12-08T02:02:26Z

Content type: article

Language: en

Sources: [Java Advent Calendar](<https://devfeed.tech/sources/java-advent-calendar.md>)

Topics: [pattern matching](<https://devfeed.tech/topics/pattern-matching.md>), [exhaustive matching](<https://devfeed.tech/topics/exhaustive-matching.md>), [sealed interface](<https://devfeed.tech/topics/sealed-interface.md>)

Tags: [2025](<https://devfeed.tech/tags/2025.md>), [data](<https://devfeed.tech/tags/data.md>), [enum](<https://devfeed.tech/tags/enum.md>), [exhaustive](<https://devfeed.tech/tags/exhaustive.md>), [expression](<https://devfeed.tech/tags/expression.md>), [hierarchy](<https://devfeed.tech/tags/hierarchy.md>), [interface](<https://devfeed.tech/tags/interface.md>), [java](<https://devfeed.tech/tags/java.md>), [json](<https://devfeed.tech/tags/json.md>), [pattern-matching](<https://devfeed.tech/tags/pattern-matching.md>), [records](<https://devfeed.tech/tags/records.md>), [sealed](<https://devfeed.tech/tags/sealed.md>), [switch](<https://devfeed.tech/tags/switch.md>)

### AI overview

Java pattern matching is most useful when the data is modeled as a sealed hierarchy of interfaces and records; the excerpt says this style is worthwhile when it fits the problem domain. A JSON example represents leaves as records or finite enums and other types as sealed interfaces, then uses a switch expression with record and constant patterns. The switch is exhaustive, as switch expressions must cover every possible selector value. The excerpt introduces antipatterns and legacy switch corner cases but does not provide their details.

### Source excerpt

Since Java 14, the Java switch and instanceof statements have been enhanced, in multiple phases, to support pattern matching and a "data-oriented" programming style. In this article, I explore when this programming style is beneficial, and why. I look at the sweet spot of perfect pattern usage, absolute antipatterns where it should not be used, [...] The post Nice and Naughty Cases of Pattern Matching appeared first on JVM Advent.

## Automating Exhaustive Branch Coverage for Sealed Types in Kotlin

DevFeed: [Automating Exhaustive Branch Coverage for Sealed Types in Kotlin](<https://devfeed.tech/articles/automating-exhaustive-branch-coverage-for-sealed-types-in-kotlin-20458.md>)

Original publisher: [Read original article](<https://eng.wealthfront.com/2025/11/20/sealed-tests/>)

Author: Sean Amos

Published: 2025-11-20T16:33:33Z

Content type: article

Language: en

Sources: [Wealthfront](<https://devfeed.tech/sources/wealthfront.md>)

Topics: [Unit testing](<https://devfeed.tech/topics/unit-testing.md>), [Software Testing](<https://devfeed.tech/topics/software-testing.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [sealed interface](<https://devfeed.tech/topics/sealed-interface.md>), [Programming Language Type Systems](<https://devfeed.tech/topics/programming-language-type-systems.md>), [exhaustive matching](<https://devfeed.tech/topics/exhaustive-matching.md>), [Code quality](<https://devfeed.tech/topics/code-quality.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [automation](<https://devfeed.tech/tags/automation.md>), [code-quality](<https://devfeed.tech/tags/code-quality.md>), [code-review](<https://devfeed.tech/tags/code-review.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [exhaustive](<https://devfeed.tech/tags/exhaustive.md>), [interfaces](<https://devfeed.tech/tags/interfaces.md>), [junit](<https://devfeed.tech/tags/junit.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [parameterized-tests](<https://devfeed.tech/tags/parameterized-tests.md>), [runner](<https://devfeed.tech/tags/runner.md>), [sealed-classes](<https://devfeed.tech/tags/sealed-classes.md>), [test-coverage](<https://devfeed.tech/tags/test-coverage.md>), [testing](<https://devfeed.tech/tags/testing.md>), [unit-testing](<https://devfeed.tech/tags/unit-testing.md>), [wealthfront-engineering](<https://devfeed.tech/tags/wealthfront-engineering.md>)

### AI overview

Kotlin's sealed types let the compiler enforce exhaustive handling in `when` expressions, but JUnit does not ensure that unit tests cover every subtype passed to a function. Parameterized tests still depend on authors to supply the cases, and code review can miss omissions. The excerpt introduces a custom "Exhaustive Test Runner" intended to ensure at least one test case for each subclass of a sealed type, including subtypes added later. The supplied text ends as the article begins describing how to build it, so it does not establish the runner's implementation or usage details.

### Source excerpt

We take code correctness and code quality seriously at Wealthfront. One of the most effective ways we ensure that our code is correct is through rigorous unit testing and strong typing. In Kotlin, sealed classes and sealed interfaces offer a powerful tool to model restricted class hierarchies. They provide strong guarantees about the types in... Read more

## Exhaustive branch checks with TypeScript

DevFeed: [Exhaustive branch checks with TypeScript](<https://devfeed.tech/articles/exhaustive-branch-checks-with-typescript-52577.md>)

Original publisher: [Read original article](<http://www.jackfranklin.co.uk/blog/typescript-exhaustive-branches/>)

Published: 2024-03-04T00:00:00Z

Content type: tutorial

Language: en

Sources: [Jack Franklin](<https://devfeed.tech/sources/jack-franklin.md>)

Topics: [TypeScript](<https://devfeed.tech/topics/typescript.md>), [exhaustive matching](<https://devfeed.tech/topics/exhaustive-matching.md>), [union types](<https://devfeed.tech/topics/union-types.md>), [enum](<https://devfeed.tech/topics/enum.md>)

Tags: [argument](<https://devfeed.tech/tags/argument.md>), [branch](<https://devfeed.tech/tags/branch.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [enum](<https://devfeed.tech/tags/enum.md>), [error](<https://devfeed.tech/tags/error.md>), [exhaustive](<https://devfeed.tech/tags/exhaustive.md>), [function](<https://devfeed.tech/tags/function.md>), [parameter](<https://devfeed.tech/tags/parameter.md>), [switch](<https://devfeed.tech/tags/switch.md>), [type](<https://devfeed.tech/tags/type.md>), [typescript](<https://devfeed.tech/tags/typescript.md>), [union](<https://devfeed.tech/tags/union.md>)

### AI overview

When branching on a TypeScript union or enum, add a `default` branch that calls `ensureExhaustive(value)`, a helper accepting `never`. If the type later gains a value that the branches do not handle, TypeScript reports that the value is not assignable to `never` and identifies the missing variant. This can catch unhandled cases even in `void` functions, where a missing branch may otherwise produce no error. The excerpt says the same approach works with narrowed `if`/`else` branches. The helper throws at runtime if reached.

### Source excerpt

It's very common when working with TypeScript that you will have a type that declares a list of values, such as an enum or union type: enum SupportedColour1 { RED, YELLOW, BLUE, } type SupportedColour2 = 'RED' | 'YELLOW' | 'BLUE' And then you will often have functions that need to run differently or return different values based on what variant is passed in: function codeForColour1(colour: SupportedColour1): string { switch (colour) { case SupportedColour1.BLUE: return '#0000ff' case SupportedColour1.RED: return '#ff0000' case SupportedColour1.YELLOW: return '#ffff00' } } function codeForColour2(colour: SupportedColour2): string { switch (colour) { case 'BLUE': return '#0000ff' case 'RED': return '#ff0000' case 'YELLOW': return '#ffff00' } } Updating code when types change One thing that can catch you out is if the list of valid values in the type changes. Let's say we want to add GREEN as a value to our union type (I will drop the enum example for brevity, but the functionality for the sake of this post is equivalent): type SupportedColour2 = 'RED' | 'YELLOW' | 'BLUE' | 'GREEN' In this case we will now get an error in this function. TypeScript will realise that the function does not return anything in the case that GREEN is passed in, but the error is not very obvious: function codeForColour2(colour: SupportedColour2): string { switch (colour) { case 'BLUE': return '#0000ff' case 'RED': return '#ff0000' case 'YELLOW': return '#ffff00' } } Function lacks ending return statement and return type does not include 'undefined'. If the function did not return a value, TypeScript will not error and it would be easy to miss that we now have a branch (for GREEN) that is not dealt with: // This will not error in TypeScript, even though we are not // dealing with the GREEN case. function logForColour(colour: SupportedColour2): void { switch (colour) { case 'BLUE': console.log('User picked blue') case 'RED': console.log('User picked red') case 'YELLOW': console.log('User picked

## Exhaustive matching in TypeScript

DevFeed: [Exhaustive matching in TypeScript](<https://devfeed.tech/articles/exhaustive-matching-in-typescript-48191.md>)

Original publisher: [Read original article](<https://tkdodo.eu/blog/exhaustive-matching-in-type-script>)

Published: 2020-08-09T00:00:00Z

Content type: article

Language: en

Sources: [TkDodo](<https://devfeed.tech/sources/tkdodo-s-blog.md>)

Topics: [TypeScript](<https://devfeed.tech/topics/typescript.md>), [exhaustive matching](<https://devfeed.tech/topics/exhaustive-matching.md>), [pattern matching](<https://devfeed.tech/topics/pattern-matching.md>), [Code](<https://devfeed.tech/topics/code.md>), [Front end](<https://devfeed.tech/topics/frontend.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>)

Tags: [compiler](<https://devfeed.tech/tags/compiler.md>), [exhaustive-matching](<https://devfeed.tech/tags/exhaustive-matching.md>), [frontend](<https://devfeed.tech/tags/frontend.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [pattern-matching](<https://devfeed.tech/tags/pattern-matching.md>), [typescript](<https://devfeed.tech/tags/typescript.md>)

### AI overview

This article explains exhaustive matching in TypeScript and how compiler checks can reveal unhandled states when developers add new branches or shapes. It contrasts exhaustive matching with default cases and discusses the risks of silently handling newly introduced states incorrectly.

### Source excerpt

Keep reading.