effect icon indicating copy to clipboard operation
effect copied to clipboard

From Discord: Error with `Match` Usage After Upgrading from 3.13 to 3.16

Open effect-bot opened this issue 6 months ago • 2 comments

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 Match usage.
  • The error arises because the expected type is string, but the code now results in a string | Refinement<unknown, string>.
  • The user is uncertain whether they are misusing Match or if this is a bug in the library.

Discord thread

https://discord.com/channels/795981131316985866/1380226090487775304

effect-bot avatar Jun 06 '25 08:06 effect-bot

I did some git bisecting and tracked the problem down to this commit: https://github.com/Effect-TS/effect/commit/bc7efa3b031bb25e1ed3c8f2d3fb5e8da166cadc#diff-956ed42947ebb72a495a619a3989f3ee9b1d17f5f7575fe3e12690278e6e8483

chrislambe avatar Jun 06 '25 15:06 chrislambe

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).

chrislambe avatar Jun 06 '25 16:06 chrislambe