microsoft / microsoft/TypeScript

Mixin with union as base class

Open
#41,132 3 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 4.1.0-dev.20201015

Search Terms:

Code

abstract class Base {
    abstract p1: number;
}

class Derived1 extends Base {
    p1 = 1;
    p2 = 2;
}

class Derived2 extends Base {
    p1 = 1;
    p3 = 3;
}

// normally TBase could extend from Base but Base is abstract here, hence the union
function Mixin<TBase extends (new (...params: any[]) => Derived1) | (new (...params: any[]) => Derived2)>(Base: TBase) {
    return class extends Base {
        p4 = 4;
        m() {
            console.log(this.p4);
            // error: Property 'p1' does not exist on type '(Anonymous class)'.(2339)
            // I'd expext the property to be available since both Derived1 and Derived2 have it
            console.log(this.p1);
        }
    };
}

const Derived1WithMixin = Mixin(Derived1);
new Derived1WithMixin().m();

Expected behavior: I'd expect the property p1 to be available in the Mixin since both types in the union have it.

Actual behavior: I get the compiler error error: Property 'p1' does not exist on type '(Anonymous class)'.(2339).

Playground Link: Link

Related Issues:

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

Start with the linked TypeScript Playground reproduction and verify the reported error for TypeScript 4.1.0-dev.20201015. Trace the compiler behavior for a mixin whose base constructor type is a union, then determine whether the shared property should be available. Done means the reproduction behaves as expected and coverage verifies the corrected behavior.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.