API Declaration loses type safety
See original GitHub issueHi,
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:
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 returnsany
instead ofFileNode
.statement
is a rule that returnsany
instead ofIfStatementNode
.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:
- Created 4 years ago
- Comments:29 (29 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I will look into this. Please open a new issue (copy paste your latest comment).
@bd82 another breaking change I found today:
OPTION
’s return type should really beT | undefined
instead ofT
.