microsoft / microsoft/TypeScript

Provide a way to specify inheritance explicitly

Open
#28,578 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
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 extends syntax 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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.