microsoft / microsoft/TypeScript
Provide a way to specify inheritance explicitly
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 3.0.0-dev.201xxxxx
Its seems there's really no way currently to setup inheritance when working with complex mixins, and/or extending prototypes manually. While mixin support landed on TypeScript recently, it only addresses very simple cases. It's usefulness ends when you also want mixin that inherit from different classes on their own. To illustrate it with an example:
export class Component extends HTMLElement implements ComponentCore {
constructor() {
super();
ComponentCore.init(this);
}
}
where the ComponentCore.init does mixin style implementation of ComponentCore. Tried the mixin approach documented, but that doesn't seem to work when you want to extend from HTMLElement here.
Now, moving on a real-life example with a lot more context: https://github.com/prasannavl/icomponent/blob/v5.1.1/packages/icomponent/src/component.ts#L6
I had to jump through hoops and do something like
ComponentCore.extend(ComponentImpl);
export interface IComponent extends ComponentImpl, ComponentStatics, ComponentCore, Constructor<IComponent> { }
export const Component: IComponent = ComponentImpl as any;
in order to even get inheritance working properly. This is further complicated, when you extend from these classes, since there's currently it's const Component, and as such it is not always interpreted by the compiler as a class -- As as example - a few lines down in the same: https://github.com/prasannavl/icomponent/blob/v5.1.1/packages/icomponent/src/component.ts#L30
You can't pass Component into generics parameters, since it's no longer a class.
Possible solutions:
- A simple and effective solution would be to just be even annotate the class and having different parents.
// @ts-inherit: IComponentCore, HTMLElement, etc, - Extend the
extendssyntax to inherit multiple classes with say "extends X, !Y, !Z` -- The exclamation is the indicator of these special classes - Allow implements to inherit a class without actually implementing it, with the same as above syntax. This is probably the least intrusive way, but unlike
extends, it forces you explicitly define interfaces to be able to do so.
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
The issue names no TypeScript repository file or test entry point; begin by reproducing the documented mixin approach and reviewing the linked icomponent example. Compare the proposed inheritance mechanisms and define which complex mixin and generic-class cases must be supported before locating compiler tests. Done means an agreed design with coverage for the supported cases.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 18/100