microsoft / microsoft/TypeScript

Cannot use `override` keyword to override methods of `Object` without explicitly extending `Object`

Open
#45,704 1 comment 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the case in the linked TypeScript Playground and inspect Object's declaration in lib/lib.es5.d.ts. Trace the override-checking behavior for a class without an explicit extends clause. Done means the example is accepted while override checks remain enforced for other cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.