ts-pattern icon indicating copy to clipboard operation
ts-pattern copied to clipboard

added a safe exhaustiveness check

Open jjhiggz opened this issue 1 year ago • 3 comments

I haven't done much open source, but I thought this might be a really useful function for me personally.

Here is a link to the issue that I opened

jjhiggz avatar Jan 27 '24 21:01 jjhiggz

@gvergnaud What's your opinion about this feature? We need a way to catch both compile-time and runtime errors. Currently exhaustive and otherwise provide just one of the errors (compile-time and runtime, respectively)

MatanYadaev avatar Mar 31 '24 14:03 MatanYadaev

I'm still considering it, but I'm not sure what's the best option in terms of API complexity / typesafety yet. In the meantime, this work around doesn't seem too bad:

try {
  return match(...).with(...).exhaustive()
} catch (e) {
  return defaultValue
}

gvergnaud avatar Mar 31 '24 22:03 gvergnaud

@gvergnaud it looks great imo. Are you ok with the breaking changes? Don't you prefer a backward-compatible change?

EDIT: I wasn't aware that .exhaustive() throws an error currently. I thought you suggested it as a PR. I dived into the code and found it. Maybe the solution is to mention it in the docs because I think all related issues aren't aware of that. At least I wasn't aware. And a suggestion, maybe it should throw a custom error, instead of a generic Error, so it will be easy to catch. (for example error instanceof PatternMatchingError instead of error.message.includes('Pattern matching error')

MatanYadaev avatar Apr 01 '24 05:04 MatanYadaev