microsoft / microsoft/TypeScript
Cannot use `override` keyword to override methods of `Object` without explicitly extending `Object`
Nobody has claimed this yet.
- 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
💻 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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