gvergnaud / ts-pattern

🎨 The exhaustive Pattern Matching library for TypeScript, with smart type inference.
MIT License
12.5k stars 135 forks source link

Match + exhaustive doesn't work with readonly array #271

Closed rikutiira closed 1 month ago

rikutiira commented 3 months ago

Describe the bug Hey, first of all thanks for the awesome library 🙏 I have a small issue where .match() with .exhaustive() is not working with a readonly array, I included screenshots of the issue.

match(value) // value: string | Date | readonly string[]
  .with(P.string, (value) => ...)
  .with(P.array(P.string), (value) => ...)
  .with(P.instanceOf(Date), (value) => ...)
  .exhaustive() // Complains about "Type 'NonExhaustiveError<readonly string[]>' has no call signatures."

Interestingly enough, the value is correct inside the P.array(P.string)) condition: value: string[] | readonly string[].

TypeScript playground with a minimal reproduction case https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbziAhjAxgCwDRwApwC+cAZlBCHAOQwDOAtGGjAKZQB2VAUFzAJ5gWcAGooANgFchAXji0YUYOwDmcAD5woLFABMI7MXzkKlygNoBddXAAiaFgG4eOlujEotcdPvlwAbuJSAFwigY48qBiYABQBkiwAlFxwKXAAdADuwDAxeGnyiiq4sWEJcNIAfHAAjEmp6Vk50XkeUCh8zfkmKgnFcVJllXAATHWpmdm5aUryKOzoLADyJNF2rL1wJfGDVQDMYylpLAAemCgS8sB+LNFJQA

Versions

exhaustive_readonly_array_1 exhaustive_readonly_array_3 exhaustive_readonly_array_2