-
Notifications
You must be signed in to change notification settings - Fork 13k
Open
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingThe issue relates to error messagingRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Milestone
Description
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.
fatcerberus, Tucaen, davidda, larrybahr-ocelot, tt-nghia and 1 more
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingThe issue relates to error messagingRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
[-]Misleading error message for [/-][+]Misleading error message for `super.instanceProperty`[/+]andrewbranch commentedon Sep 26, 2023
#54056
sandersn commentedon Sep 26, 2023
How is
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 commentedon Sep 26, 2023
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.
fatcerberus commentedon Sep 26, 2023
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 commentedon Sep 26, 2023
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 commentedon Sep 26, 2023
Sorry, fixed now to make it
super.roots
again.super.
for inferred class properties in JavaScript #55884