I am trying to use a 3rd party typescript library(antlr - https://github.com/tunnelvisionlabs/antlr4ts) in my angular 2 project created using angular-cli. It's failing with this error class constructor MyLexer cannot be invoked without 'new. If you look at below code snippet , it's failing while making call to super();.Also Lexer.js is sitting in node_modules of antlr library.
I am not using babel in my project but I found this other stackoverflow post which has similar error as mine Babel error: Class constructor Foo cannot be invoked without 'new'. It says that due to the way ES6 classes work, you cannot extend a native class with a transpiled class. Is it somehow related to my issue as well? Please guide.
Code snippet
let inputStream = new ANTLRInputStream("sometext");
let lexer = new MyLexer(inputStream); // it fails here
let tokenStream = new CommonTokenStream(lexer);
let parser = new MyParser(tokenStream);
MyLexer.ts (Generated code)
export class MyLexer extends Lexer {
constructor(input: CharStream) {
super(input); // it fails here.
this._interp = new LexerATNSimulator(MyLexer._ATN, this);
}
// more code
}
Lexer.js (sitting in node_modules)
class Lexer extends Recognizer_1.Recognizer {
constructor(input) {
super();
//more code
No, this is not possible. Constructors that are created using the class keyword can only be constructed with new , if they are [[call]]ed without they always throw a TypeError 1 (and there's not even a way to detect this from the outside).
A constructor of a class is a special method that gets called when a class is instantiated using the NEW function. A constructor for a class has the same name as the class name. Unlike ordinary methods, a constructor definition is identified by the CONSTRUCTOR statement.
To fix JavaScript ES6 TypeError: Class constructor Client cannot be invoked without ‘new’, we need to transpile our code to ES6 or later. { "compilerOptions": { "target": "ES6" //... } } to set the target to 'ES6' to transpile the code to ES6 so ES6 classes are kept after transpiling.
Transpiled classes cannot extend native classes, at least without additional measures. Since ES6 classes should be only called with new, NativeBar.call results in error.
All other TS generated classes work as fine and everything below CObject fails when it gets to calling the CObject constructor.
You might also be getting the error if you forgot to use the new operator when instantiating a class in TypeScript. To get around this, always make sure to use the new operator when creating instances of a class. We used the new operator upon class instantiation. The new operator does 3 things:
That's quite surprising. I'm using the same approach like you in my node module antlr4-graps, which has ES6 set as transpilation target and everything is working very well. Take a closer look there. Maybe you can spot other signifcant differences between your code and mine. I assume you have the latest alpha of antlr4ts installed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With