microsoft / microsoft/TypeScript
Type narrowing of `super` when using `this`
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
super, this, class
🕗 Version & Regression Information
Nightly
⏯ Playground Link
💻 Code
class Animal<T> {
eat<T>(this: Animal<T>) {
throw new Error('This doesnt matter');
}
}
class Dog<T> extends Animal<T> {
eat<U>(this: Dog<U>) {
super.eat(); // The type is Animal<T>.eat<T>(this: Animal<T>): void
// The type should be Animal<U>.eat<U>(this: Animal<U>): void
}
}
🙁 Actual behavior
super.eat() is typed as Animal<T>.eat<T>(this: Animal<T>): void
🙂 Expected behavior
super.eat() should be typed as Animal<U>.eat<U>(this: Animal<U>): void since this is declared as Dog<U> which is a subclass of Animal<U>.
One can workaround this by writing super.eat<U>(), but the signature then becomes goofy (it becomes Animal<T>.eat<U>(this: Animal<U>): void)
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
Start with the linked TypeScript Playground and reproduce the difference between the current and expected types for super.eat() in the generic Animal and Dog classes. Trace the compiler's handling of this types in super property access, then add a regression test and confirm that super.eat() preserves the U-specific type without requiring an explicit type argument.
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
- Clearly specified
- Newbie friendliness
- 38/100