question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API Declaration loses type safety

See original GitHub issue

Hi,

I just started using this library, and I’m impressed by the performance! However, there were a few issues that I’m struggling with when it comes to typescript type safety. For example, considering this API:

https://github.com/SAP/chevrotain/blob/5c026d34bbe8723d08cb1eb63ddedd710737cb99/packages/chevrotain/api.d.ts#L894-L898

T | any to the compiler is just any, which means that all rules now return any instead of the intended T. This makes it impossible to write the rule class in a type safe manner. Which means that for an example grammar:


export class MyParser extends chevrotain.Parser {
  public constructor() {
    super(MyTokens, {
      recoveryEnabled: true,
      outputCst: false,
    });

    this.performSelfAnalysis();
  }

  public file = this.RULE('file', () => {
    const statements = Array<BaseStatementNode>();
    this.MANY(() => {
      statements.push(this.SUBRULE(this.statement));
    });
    return new FileNode(statements);
  });

  public statement = this.RULE('statement', () => {
    // TODO: get members
    return new IfStatementNode(...members);
  });
}
  • file is a rule that returns any instead of FileNode.
  • statement is a rule that returns any instead of IfStatementNode.
  • SUBRULE has the same problem since it relies on the argument type.
  • Most other production helpers are affected as well since type inference is broken.

Is this an intentional design decision? or is it just a bug with the type definition? there might be other APIs that are defined in this manner as well.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:29 (29 by maintainers)

github_iconTop GitHub Comments

1reaction
bd82commented, Jun 6, 2019

I will look into this. Please open a new issue (copy paste your latest comment).

1reaction
OmarTawfikcommented, May 30, 2019

@bd82 another breaking change I found today: OPTION’s return type should really be T | undefined instead of T.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript Failed to compile because Type declaration of 'any ...
tsx (14,20): Type declaration of 'any' loses type-safety. Consider replacing it with a more precise type. This error occurred during the build ...
Read more >
API Declaration loses type safety · Issue #964 · Chevrotain ... - GitHub
Hi, I just started using this library, and I'm impressed by the performance! However, there were a few issues that I'm struggling with...
Read more >
TypeScript Failed to compile because Type declaration of 'any ...
Coding example for the question TypeScript Failed to compile because Type declaration of 'any' loses type-safety-Reactjs.
Read more >
Methods for TypeScript runtime type checking - LogRocket Blog
Explore five methods of performing TypeScript type checks at runtime in this post and learn each of their advantages and disadvantages.
Read more >
The C# type system | Microsoft Learn
The compiler uses type information to make sure all operations that are performed in your code are type safe. For example, if you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found