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

Improved type-safety for OR #1986

Open
LaurensRietveld opened this issue Sep 6, 2023 · 2 comments
Open

Improved type-safety for OR #1986

LaurensRietveld opened this issue Sep 6, 2023 · 2 comments

Comments

@LaurensRietveld
Copy link

LaurensRietveld commented Sep 6, 2023

Take this OR statement:

const result = this.OR([
  {ALT: () => 1},
  {ALT: () => 'string'}
])

The type of result is any, where I'd expect it to be of type string | number. Is there a reason why the returntype of the ALT functions isn't used instead?

ps. Many thanks for this library! Enjoying the type safety and performance

@LaurensRietveld
Copy link
Author

LaurensRietveld commented Sep 6, 2023

Note that I managed to work around this myself by extending the OR function and its types:

  public OR<A extends Array<IOrAlt<any>>>(alt: A) {
    return super.OR(alt) as ReturnType<A[number]["ALT"]>;
  }

I understand that my types are incomplete (I'm only including the IOrAlt types and not the OrMethodOpts types), but it suffices for my usecase

@bd82
Copy link
Member

bd82 commented Dec 31, 2023

Hello @LaurensRietveld

Could this be extended in a generic manner to improve the Chevrotain types?

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