Skip to content

override keyword not honored for methods of Object on classes with no extends, Object super members missing #49224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
trusktr opened this issue May 24, 2022 · 6 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@trusktr
Copy link
Contributor

trusktr commented May 24, 2022

Bug Report

All classes without an extends clause implicitly extend from Object. However the override keyword does not work when overriding members of Object, and I believe it should for consistency, semantics, and display of developer intent.

image

Furthermore, TypeScript chooses to hide super members of Object, and the following valid JavaScript has an unexpected type error:

class Foo {
  toString() {
      return super.toString() + ' ' + 123 // Error: 'super' can only be referenced in a derived class. (2335)
  }
}

new Foo().toString() // This works just fine in JavaScript.

The class is implicitly derived (from Object), and super.toString exists.

🔎 Search Terms

🕗 Version & Regression Information

I don't think it ever worked?

⏯ Playground Link

Playground link with relevant code

Playground link with relevant code

🙁 Actual behavior

No type information is present for inherited Object members inside a class without extends.

Possibly related: even on any plain object like a POJO, intellisense in VS Code does not show helpful autocompletion for Object members.

🙂 Expected behavior

It should show the full picture, because it is valid JavaScript.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels May 24, 2022
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented May 24, 2022

This would be a breaking change for people who have a custom toString and turned on noImplicitOverride.

It seems like if you're explicit enough to be specifying that toString is an override, then writing class Foo extends Object should also be a thing you'd want to write.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented May 24, 2022

This would be a breaking change for people who have a custom toString and turned on noImplicitOverride.

This doesn't necessarily require issuing an error on noImplicitOverride, though the workaround would be relatively simple for codebases who are opting themselves into noImplicitOverride. It might be a breaking change for .d.ts that don't at least allow specifying override on Object members.

Really what I'm getting is that we should at least allow specifying override if a member is declared on Object. If we wanted to have a two phase process of

  1. first allowing override and then
  2. erroring on its absence in noImplicitOverride

that might not be a bad idea.

In the meantime though, an explicit extends Object does seem like a reasonable workaround.

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented May 24, 2022

noImplicitOverride today has the goodness that it works just like jumping in checkers: If you can write override, you must

Turning that from a boolean (must write, cannot write) to a tristate (must write, may write, cannot write) is a big can of worms.

@DanielRosenwasser
Copy link
Member

I forgot about the fact that override gets stripped away in .d.ts files, so I think you're right, it might be best to keep the two in sync.

@myknbani
Copy link

myknbani commented Jun 9, 2022

@RyanCavanaugh

This would be a breaking change for people who have a custom toString and turned on noImplicitOverride.

It seems like if you're explicit enough to be specifying that toString is an override, then writing class Foo extends Object should also be a thing you'd want to write.

how about another compiler option like "implicitExtendObject" 🥴 😁 ?

@trusktr
Copy link
Contributor Author

trusktr commented Aug 26, 2022

It seems like if you're explicit enough to be specifying that toString is an override, then writing class Foo extends Object should also be a thing you'd want to write.

Every JS programmer should know that class Foo {} extends from Object by default (because it effectively does!). But that's a good workaround for base class authors only too.

Some people are importing classes from libraries, for example, and forking a library just to add extends Object to a library's base class is not ideal.

What about an new option noImplicitObjectOverrides that defaults to false, then deprecate implicit Object overrides, and eventually switch the default to true (at least for strict mode)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants