Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class constructor cannot be invoked without 'new

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
like image 417
user911 Avatar asked May 01 '17 20:05

user911


People also ask

Can constructors be called without new?

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).

What is a class constructor?

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.

How to fix JavaScript ES6 TypeError class constructor client cannot be invoked?

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.

Can transpiled classes extend native classes?

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.

Is it possible to use TS generated classes in cobject?

All other TS generated classes work as fine and everything below CObject fails when it gets to calling the CObject constructor.

Why am I getting error when instantiating a class in typescript?

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:


1 Answers

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.

like image 167
Mike Lischke Avatar answered Sep 22 '22 01:09

Mike Lischke