microsoft / microsoft/TypeScript

Intersection type of abstract classes does not throw error if same member exists in multiple abstract classes

Open
#26,617 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Intersection Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.0.1

Search Terms: "Intersection", "abstract"

Code

export type Constructor<T = {}> = new (...args: any[]) => T;

abstract class Outer1 {
  abstract same: number;
  abstract notSame: string;
}

abstract class Outer2 {
  abstract same: number;
}

function Mixin1<TBase extends Constructor>(Base: TBase) {
  abstract class Inner1 extends Base {   // Same as Outer1
    abstract same: number;
  }
  return Inner1;
}

function Mixin2<TBase extends Constructor>(Base: TBase) {
  abstract class Inner2 extends Base {   // Same as Outer2
    abstract same: number;
  }
  return Inner2;
}

function rollUp<T>(mixins): new (...args) => T {
  return mixins.reduce((acc, mixin) => mixin(acc), class Seed {});
}

class Example extends rollUp<Outer1 & Outer2>([Mixin1, Mixin2]) {
  // Compiler will throw error when "notSame" is unimplemented
  // Compiler DOES NOT throw error about non-implemented "same" member
}

Expected behavior:
Compiler should say that same is not implemented in regular class

Actual behavior:
Compiler does not throw error saying that same is not present in example class, despite throwing an error about notSame. It will throw an error, however, if the types of same are incompatible.

Playground Link: https://www.typescriptlang.org/play/#src=export%20type%20Constructor%3CT%20%3D%20%7B%7D%3E%20%3D%20new%20(...args%3A%20any%5B%5D)%20%3D%3E%20T%3B%0D%0A%0D%0Aabstract%20class%20Outer1%20%7B%0D%0A%20%20abstract%20same%3A%20number%3B%0D%0A%20%20abstract%20notSame%3A%20string%3B%0D%0A%7D%0D%0A%0D%0Aabstract%20class%20Outer2%20%7B%0D%0A%20%20abstract%20same%3A%20number%3B%0D%0A%7D%0D%0A%0D%0Afunction%20Mixin1%3CTBase%20extends%20Constructor%3E(Base%3A%20TBase)%20%7B%0D%0A%20%20abstract%20class%20Inner1%20extends%20Base%20%7B%20%20%20%2F%2F%20Same%20as%20Outer1%0D%0A%20%20%20%20abstract%20same%3A%20number%3B%0D%0A%20%20%7D%0D%0A%20%20return%20Inner1%3B%0D%0A%7D%0D%0A%0D%0Afunction%20Mixin2%3CTBase%20extends%20Constructor%3E(Base%3A%20TBase)%20%7B%0D%0A%20%20abstract%20class%20Inner2%20extends%20Base%20%7B%20%20%20%2F%2F%20Same%20as%20Outer2%0D%0A%20%20%20%20abstract%20same%3A%20number%3B%0D%0A%20%20%7D%0D%0A%20%20return%20Inner2%3B%0D%0A%7D%0D%0A%0D%0Afunction%20rollUp%3CT%3E(mixins)%3A%20new%20(...args)%20%3D%3E%20T%20%7B%0D%0A%20%20return%20mixins.reduce((acc%2C%20mixin)%20%3D%3E%20mixin(acc)%2C%20class%20Seed%20%7B%7D)%3B%0D%0A%7D%0D%0A%0D%0Aclass%20Example%20extends%20rollUp%3COuter1%20%26%20Outer2%3E(%5BMixin1%2C%20Mixin2%5D)%20%7B%0D%0A%20%20%2F%2F%20Compiler%20will%20throw%20error%20when%20%22notSame%22%20is%20unimplemented%0D%0A%20%20%2F%2F%20Compiler%20DOES%20NOT%20throw%20error%20about%20non-implemented%20%22same%22%20member%0D%0A%7D

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 by running the linked TypeScript Playground reproduction and compare the diagnostics for same and notSame in the Example class. Trace the compiler's handling of abstract members produced by the intersection and mixins; done means the missing same member is reported while the existing notSame diagnostic remains correct.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.