Skip to content

Misleading error message for super.instanceProperty #55883

@DanielRosenwasser

Description

@DanielRosenwasser
Member

Take the following code:

export abstract class YaddaBase {
    protected roots = "hi";
}

export class DerivedYadda extends YaddaBase {
    public get rootTests() {
        return super.roots;
    }
}

TypeScript nightly (5.3.0-dev.20230926) reports:

Class field 'roots' defined by the parent class is not accessible in the child class via super.

The error message here is confusing - one might believe that this has to do with accessing a member with a protected modifier, but it is really about accessing an instance field with super. A better error message would be:

'roots' is defined as an instance property and must be accessed through 'this', not 'super'.

We can also provide a quick fix for this error message.

Activity

changed the title [-]Misleading error message for [/-] [+]Misleading error message for `super.instanceProperty`[/+] on Sep 26, 2023
andrewbranch

andrewbranch commented on Sep 26, 2023

@andrewbranch
Member
sandersn

sandersn commented on Sep 26, 2023

@sandersn
Member

How is

Class field 'roots' is defined by the super class and must be accessed through 'this', not 'super'.

I don't understand the advantage of calling 'roots' a class field vs instance property, so maybe "Instance property 'roots' is ..." is better.
I definitely think 'super class' is more accurate than 'parent class' and more a propos than 'base class' since super is the mistaken keyword in question.

DanielRosenwasser

DanielRosenwasser commented on Sep 26, 2023

@DanielRosenwasser
MemberAuthor

Because it's not really about class fields, it's about instance properties.

Though for what it's worth, we don't issue the error in JavaScript for properties inferred from assignments.

// @ts-check

class YaddaBase {
    constructor() {
        this.roots = "hi";
    }
}

class DerivedYadda extends YaddaBase {
    get rootTests() {
        return super.roots; // no error?
    }
}

console.log(new DerivedYadda().rootTests); // prints undefined!
fatcerberus

fatcerberus commented on Sep 26, 2023

@fatcerberus

I prefer “instance property”, as that is the important distinction: it’s not accessible through super precisely because it’s a property of the instance and not the prototype.

fatcerberus

fatcerberus commented on Sep 26, 2023

@fatcerberus

Wait, why does that print undefined? Shouldn’t the base class ctor run before the getter?

Ignore this, above example was edited. Leaving comment for posterity.

DanielRosenwasser

DanielRosenwasser commented on Sep 26, 2023

@DanielRosenwasser
MemberAuthor

Sorry, fixed now to make it super.roots again.

added
RescheduledThis issue was previously scheduled to an earlier milestone
on Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingRescheduledThis issue was previously scheduled to an earlier milestone

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sandersn@DanielRosenwasser@andrewbranch@fatcerberus@RyanCavanaugh

        Issue actions

          Misleading error message for `super.instanceProperty` · Issue #55883 · microsoft/TypeScript