Skip to content

Extending intersection types #10549

@Neftedollar

Description

@Neftedollar

Add support of extending intersection types

Example

class Person1 {
 foo() {
  retorn "person1";
 }
}

class Person2 {
 bar() {
  retorn "person2";
 }
}

class Person extends Person1 & Person2 { 
 foobar() {
  retorn "person3";
 }
}

// or 
// type Persons = Person1 & Person2
// class Person extend Persons {}

var person = new Person();
person.foobar() //person3
person.bar() //person2
person.foo() //person1

Use case

For example, I have a class AngularComponent

export class AngularComponent  {
    someLogic(){ 
         /* .. */
    }
    public goBack(){
        window.history.back();
    }
    public goForward(){
        window.history.forward();
    }
} 

This component use history API for navigation. This is not only component who use this logic, so I dont repeat myself and create HistoryNavigationClass

export class HistoryNavigationClass  {
    public goBack(){
        window.history.back();
    }
    public goForward(){
        window.history.forward();
    }
} 

but some of my components does not use goForward() method, so I split HistoryNavigationClass into two separated classes HistoryBackWalker and HistoryForwardWalker

export class HistoryBackWalker  {
    public goBack(){
        window.history.back();
    }
} 
export class HistoryForwardWalker  {
   public goForward(){
        window.history.forward();
    }
}

and then I try to extend AngularComponent from intersection of HistoryBackWalker and HistoryForwardWalker type.

//type HistoryWalker = HistoryBackWalker & HistoryForwardWalker;
export class AngularComponent  extends HistoryBackWalker & HistoryForwardWalker  { // or   extends HistoryWalker
    someLogic(){ 
         /* .. */
    }
} 

but I can't.

With JavaScript

I can do this:

var Person1 = function(){};
Person1.prototype.foo = function(){return "person1";};

var Person2 = function(){};
Person2.prototype.bar = function(){return "person2";};

var Person = function(){};
Person.prototype.foobar = function(){return "person3";};

Person.prototype = Object.assign(Person.prototype,Person1.prototype,Person2.prototype);

var person = new Person();
person.foo();  // "person1"
person.bar();  //"person2"
person.foobar(); // "person3"

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions