Repository navigation
Misleading error message for super.instanceProperty #55883
Description
Activity
- changed the title
[-]Misleading error message for [/-][+]Misleading error message for `super.instanceProperty`[/+]on Sep 26, 2023 - addedBugA bug in TypeScriptA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingThe issue relates to error messaging
on Sep 26, 2023 andrewbranch commented
on Sep 26, 2023 MemberMore actionsReacted by Daniel Rosenwassersandersn commented
on Sep 26, 2023 MemberMore actionsHow 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' sincesuperis the mistaken keyword in question.DanielRosenwasser commented
on Sep 26, 2023 MemberAuthorMore actionsBecause 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!
I prefer “instance property”, as that is the important distinction: it’s not accessible through
superprecisely because it’s a property of the instance and not the prototype.Reacted by Daniel Rosenwasser, Nathan Shively-Sanders and tt-nghiaWait, 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.
Reacted by Daniel RosenwasserDanielRosenwasser commented
on Sep 26, 2023 MemberAuthorMore actionsSorry, fixed now to make it
super.rootsagain.Reacted by Bruce Pascoe- assigned and unassigned
on Sep 28, 2023 - addedRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
on Jul 26, 2024
Take the following code:
TypeScript nightly (5.3.0-dev.20230926) reports:
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:We can also provide a quick fix for this error message.