gvergnaud / ts-pattern

🎨 The exhaustive Pattern Matching library for TypeScript, with smart type inference.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TS error when declaring "pattern" using P.string as a variable.

momentiris opened this issue · comments

Hello and thanks for the great work on this library!

I'm currently bumping TS and ts-pattern to v5 and ran into an unexpected issue.
Prior to v5, I've had some patterns declared to variables, like this:

const isFoo = {
  type: 'foo',
  data: P.string
}

match(unknown)
  .with(isFoo, (foo) => ...)
  .otherwise(() => ...)

After upgrading ts and ts-pattern, I now get an error on isFoo:

The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed. (tsserver 7056)

Removing the P.string also removes the error
Using P.string "inline" does not give this error.

ts-pattern 5.0.1
TS 5.1.6

Any ideas?

Thanks in advance.

Hey, could you provide a playground with a reproduction case? I do not reproduce this issue: Playground

Btw it's recommended to use as const at the end of your pattern declaration for better type inference:

const isFoo = {
  type: 'foo',
  data: P.string
} as const

That's strange. I realised now that the error actually arises when you export isFoo. I didn't include that in my initial example, but it's something I do in the project where I'm facing the issue.

playground with export

Thanks to the help of the amazing @Andarist, I think I have a fix: #175

I'll release this tomorrow

Look promising! Thanks for the quick assistance!

Fixed in v5.0.3