DEV Community

Ahmad Tibibi
Ahmad Tibibi

Posted on

TS1034: 'super' must be followed by an argument list or member access

Understanding TypeScript and the TS1034 Error: 'super' must be followed by an argument list or member access

What is TypeScript?

TypeScript is a typed superset of JavaScript (JS), which means it extends JavaScript by adding static types. This allows developers to catch errors early during development and enhances the overall maintainability of code. One of TypeScript's key features is its ability to define types, interfaces, and enums, which help in creating robust applications.

Types are essentially labels that specify what kind of values a variable can hold. They can be numbers, strings, booleans, objects, and more. This type system allows TypeScript to provide a stronger foundation for developing large applications where type safety is crucial.

Understanding the 'super' Keyword

In TypeScript (and JavaScript), the super keyword is used in derived classes to access and call functions on an object's parent class. This keyword must be used correctly to maintain proper inheritance (the mechanism by which one class can inherit properties and methods from another class).

TS1034: 'super' must be followed by an argument list or member access

The TS1034 error occurs when the super keyword is used incorrectly in a subclass. To correctly invoke the parent class methods or access its properties, super must either be followed by a method call (with parentheses) or a member access (a property or method of the parent).

Examples of Errors

Let’s look at several examples that would cause the TS1034 error along with explanations and corrections.

Example 1: Inappropriate Use of super

class Animal {
    constructor(public name: string) {}
    speak() {
        console.log(`${this.name} makes a noise.`);
    }
}

class Dog extends Animal {
    constructor(name: string) {
        super; // TS1034: 'super' must be followed by an argument list or member access
        this.name = name;
    }
}
Enter fullscreen mode Exit fullscreen mode

In this case, the super keyword is not being called as a function. To fix this, we need to provide an argument list to the super:

class Dog extends Animal {
    constructor(name: string) {
        super(name); // Correct usage of super
        this.name = name;
    }
}
Enter fullscreen mode Exit fullscreen mode

Example 2: Accessing a Parent Class Method Incorrectly

class Cat extends Animal {
    constructor(name: string) {
        super; // TS1034: 'super' must be followed by an argument list or member access
    }
    speak() {
        super.speak; // TS1034: 'super' must be followed by an argument list or member access
    }
}
Enter fullscreen mode Exit fullscreen mode

To correct this, we should ensure that we call the speak method with parentheses:

class Cat extends Animal {
    constructor(name: string) {
        super(name); // Correct usage of super with argument
    }
    speak() {
        super.speak(); // Correct access with method call
    }
}
Enter fullscreen mode Exit fullscreen mode

Important Things to Know

  1. Super Keyword Usage: Always use super followed by parentheses () if you intend to call the parent constructor or a method.

  2. Error Messages: Pay close attention to error messages like TS1034, which help pinpoint exactly where the mistake is.

  3. Type Safety: Take advantage of TypeScript’s type system to catch errors early, especially when dealing with inheritance.

  4. Method Access: Remember to use parentheses when accessing methods via super, as missing them will lead to TS1034 errors.

  5. Code Readability: Keeping clear and concise code will help not only with debugging but also with understanding the flow of inheritance.

FAQs about TS1034 Error

Q: Why do I need to call super?

A: Calling super is necessary to properly initialize the parent class, especially when class properties and methods depend on inherited behavior.

Q: What other types of errors can occur with super?

A: Common issues include incorrect order of access, missing arguments, or trying to access a non-existent method/property on the parent.

Q: How can I prevent TS1034 errors?

A: Familiarize yourself with proper class inheritance syntax in TypeScript and make sure super is followed by an argument list or a property/method access.

Conclusion

The TS1034: 'super' must be followed by an argument list or member access error is a straightforward yet crucial error to understand in TypeScript. By correctly using the super keyword, you ensure that your classes inherit behavior properly, keeping your codebase efficient and maintainable. Always remember to check whether you're using super correctly—whether it’s followed by an argument list or member access—to avoid this error and write cleaner, more reliable TypeScript code.

Top comments (0)