From Discord: Error with `Match` Usage After Upgrading from 3.13 to 3.16
Summary
The user is experiencing an issue after upgrading from version 3.13 to 3.16 of a library, specifically with the Match functionality they use to refine unknown types to specific shapes. The provided example code used to work but now results in a TypeScript error. The Match.when method returns a Refinement<unknown, string> instead of a simple string, causing a type mismatch.
Key takeaways:
- There is a regression or change in type behavior from version 3.13 to 3.16 affecting
Matchusage. - The error arises because the expected type is
string, but the code now results in astring | Refinement<unknown, string>. - The user is uncertain whether they are misusing
Matchor if this is a bug in the library.
Discord thread
https://discord.com/channels/795981131316985866/1380226090487775304
I did some git bisecting and tracked the problem down to this commit:
https://github.com/Effect-TS/effect/commit/bc7efa3b031bb25e1ed3c8f2d3fb5e8da166cadc#diff-956ed42947ebb72a495a619a3989f3ee9b1d17f5f7575fe3e12690278e6e8483
As a workaround, using any as the input type instead of unknown will circumvent the typing error:
import { Match } from "effect"
const match = Match.type<any>().pipe(
Match.withReturnType<string>(),
Match.when({ a: Match.string }, (_) => _.a),
Match.orElseAbsurd
)
As opposed to using Match.type<unknown> or Match.value(foo) where foo is unknown (e.g. when matching caught errors which are implicitly unknown).