Skip to content

Cannot use override keyword to override methods of Object without explicitly extending ObjectΒ #45704

Open
@kmxz

Description

@kmxz

Bug Report

πŸ”Ž Search Terms

override, TS4112

πŸ•— Version & Regression Information

It happens in 4.4.2 and has been there since the introduction of override keyword (#39669).

⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYGwhgzhAECC0G8BQ1oHsBuBTATjglgCZbQAuaAyqQQHYDmAFAJSKo5akCuON0A5ADM0aPgG5oAXyQSgA

πŸ’» Code

class A {
  override toString() { return 'foo'; }
}

πŸ™ Actual behavior

TS4112: This member cannot have an 'override' modifier because its containing class 'A' does not extend another class.

πŸ™‚ Expected behavior

It should be allowed, since the method overrides Object.prototype.toString.

Per ECMAScript spec Β§15.7.12.7, a class without an explicit extends clause would be implicitly inheriting from Object. Therefore, such classes should be allowed to explicitly override methods declared in Object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions