Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
momentiris opened this issue Jul 13, 2023 · 5 comments
Closed

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

momentiris opened this issue Jul 13, 2023 · 5 comments

Comments

@momentiris
Copy link
Contributor

momentiris commented Jul 13, 2023

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.

@gvergnaud
Copy link
Owner

gvergnaud commented Jul 13, 2023

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

@momentiris
Copy link
Contributor Author

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

@gvergnaud
Copy link
Owner

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

I'll release this tomorrow

@momentiris
Copy link
Contributor Author

Look promising! Thanks for the quick assistance!

@gvergnaud
Copy link
Owner

Fixed in v5.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants