Typeerror: super expression must either be null or a function

Are you dealing with a “typeerror: super expression must either be null or a function” error message right now?

And you’re having a hard time fixing it?

If yes, this article is indeed for you. Want to know why?

This is because…

In this article, we are going to show how to troubleshoot this “super expression must either be null or a function” error message.

So, keep on reading.

What is “typeerror: super expression must either be null or a function”?

The “typeerror super expression must either be null or a function” is an error message that occurs in JavaScript when the “super” keyword is used incorrectly.

In addition to that, it indicates that the usage of “super” is not valid and it must either be set to null or used as a function call to refer to the parent class constructor.

Why does this error occur?

Here are the most common causes of the “super expression must either be null or a function” error.

  • Incorrect usage of “super” keyword
  • Not extending a parent class
  • Typo or misspelling

On the other hand, in object-oriented programming languages like Python or JavaScript, “super” is used to refer to the parent class or superclass from within a subclass.

However, there are certain rules that need to be followed:

→ “super” must be used within the constructor of the subclass.


→ If “super” is used with parentheses, it should be followed by arguments (if the parent class constructor requires them).


→ If “super” is used without parentheses, it refers to the parent class constructor without passing any arguments.


→ “Super” can also be used to call methods of the parent class from within the subclass.

How to fix “typeerror: super expression must either be null or a function”

Here are the different solutions you may use to fix the “super expression must either be null or a function” error immediately:

1. Use “super” keyword correctly within the constructor of the subclass

Make sure that you’re using the “super” keyword correctly within the constructor of the subclass.

The “super” should only be used within the constructor, and it should be followed by parentheses if you want to call the parent class constructor with arguments.

Example code:

class ParentClass {
  constructor(arg1) {
    // Parent class constructor code here
  }
}

class ChildClass extends ParentClass {
  constructor(arg1, arg2) {
    super(arg1); // Correctly calling parent class constructor with arg1
    this.property = arg2; // Initializing subclass's own property
  }
}

2. Check for any other incorrect usage of “super” in your subclass methods.

The “super” should only be used to call the parent class constructor or methods, and it should be used in a valid manner within the constructor or methods of the subclass.

Example code:

class ParentClass {
  someMethod() {
    // Parent class method code here
  }
}

class ChildClass extends ParentClass {
  someMethod() {
    super.someMethod(); // Correctly calling parent class method
    // Subclass method code here
  }
}

3. Ensure passing the required arguments to “super()”

Ensure that you are passing the required arguments to “super()” if the parent class constructor requires it.

When the parent class constructor has parameters, you need to pass those parameters as arguments to “super()” in the subclass constructor.

Example code:

class ParentClass {
  constructor(arg1) {
    // Parent class constructor code here
  }
}

class ChildClass extends ParentClass {
  constructor(arg1, arg2) {
    super(arg1); // Correctly calling parent class constructor with arg1
    this.property = arg2; // Initializing subclass's own property
  }
}

Note: If the above solutions do not resolve the error try to double-check for any typos or misspelling in the usage of the “super” keyword or the parent class name.

Ensure they are correctly spelled and referenced in the code to avoid error.

Conclusion

By executing the different solutions that this article has already given and making sure that you’re using the “super” keyword correctly, passing required arguments, and using it only within valid contexts.

You can definitely fix the “typeerror: super expression must either be null or a function” error in JavaScript.

We are hoping that this article provides you with sufficient solutions.

You could also check out other “typeerror” articles that may help you in the future if you encounter them.

Thank you very much for reading to the end of this article.