Typescript multiple inheritance












8















I'm need of multiple inheritance in typescript.
Logically it is not good to put a lot of functionality to hierarchy.
I have one base class and number of hierarchy branches.
But I need somehow to use mixings to put some main logic in separate classes, cuz it is not used in every branch.



UPDATE of code exmple:



function Mixin = function(mixins:any){ // mixin decorator
return function(target){
mixins.forEach((mixin) => {
add every function from mixing to target prototype,
if functions with same name does not exists there,
so we are able to do calls to as example different render() functions

Will show it in OpenableItem
});
}
}

function element = function(){
return function(target){
target.prototype.element = function(){
return this.$el;
}
}
}
--------------------------------------------------------------------------------------
@element // every item will have this function as Root as Openable
class BaseItem{
y() => number; // we need to get y position of every item
}
OpenableMixin{
render() => render arrow only
open(){} => change arrow position and cause menu to fire change event
close(){} => change arrow position and cause menu to fire change event
}
class ItemsMixin extends OpenableMixing{// for now if item have childs it must be openable,
// but Responsive has only tasks and must be openable too
addItem(item: BaseItem) // need to add generics
removeItem(item: BaseItem)
}
--------------------------------------------------------------------------------------
@Mixin([ItemsMixin, ActivitiesMixin]) // it can have items and activities
class OpenableItem extends BaseItem implement ItemsMixin, ActivitiesMixin { // as in typescript docs
render(){
// call rendering from BaseItem class
super.render();
// call rendering from OpenableMixing
OpenableMixin.prototype.render.call(this);
// do some separate rendering for OpenableItem only
this.$el.append('line');
}
}

@Mixin([ItemsMixin]) // it can have items
class RootItem extends BaseItem implement ItemsMixin{ // and subitems functionality is only for Root class
subitems: Array<BaseItem> // need to add generics to be able to put here different item types
}
--------------------------------------------------------------------------------------
@element
class Menu{
items: Array<Item>
}

@element
class Timeline{
menu: Menu
listAllelement() => {
console.log(this.element());
console.log(this.menu.element());
this.menu.items.forEach((item) => {
console.log(item.element());
if(item.hasChilds()){ // really it must be (item instanceof RootItem || item instanceof OpenableItem)
item.items.forEach((subitem) => { // really we need some recursion here
console.log(subitem.element());
})
}
})
}
}


In reality it is rare situation when you need to implement multiple inheritance, and it is much rarely when you can have such an issue in javascript. But every item can have different functionality depending on needs.



Just imagine that there can be different items which can have number of mixins. Is it wise to put everything into base ? And what is your approach to this issue ?










share|improve this question

























  • Imo you should use DI for this..

    – Dieterg
    Nov 25 '16 at 15:33











  • @LenilsondeCastro Although the idea of multiple inheritance often is dismissed in OOP, it's not really any violation of any principle, it's just that it's not trivial to implement and that we are taught that it's just a stupid idea anyway. In reality, all entities can be categorized in many different unrelated abstractions, and I think it would be neat to have first-class support for that kind of structural organization in a language.

    – Alex
    Nov 25 '16 at 15:47











  • @Dieterg Sorry I'm not sure how DI can help in such situation, but code example is updated.

    – Vayrex
    Dec 10 '16 at 19:14











  • @Alex I was having situation when multiple inheritance was good solution for small python script. And I was not using it in javascript before. But in my case I have a lot of different childs, which need to have separate functionality not only in rendering, but in subscribtions to event emitters and a lot of other things. I was searching for other solutions but mixins seems to be more logical here.

    – Vayrex
    Dec 10 '16 at 19:21
















8















I'm need of multiple inheritance in typescript.
Logically it is not good to put a lot of functionality to hierarchy.
I have one base class and number of hierarchy branches.
But I need somehow to use mixings to put some main logic in separate classes, cuz it is not used in every branch.



UPDATE of code exmple:



function Mixin = function(mixins:any){ // mixin decorator
return function(target){
mixins.forEach((mixin) => {
add every function from mixing to target prototype,
if functions with same name does not exists there,
so we are able to do calls to as example different render() functions

Will show it in OpenableItem
});
}
}

function element = function(){
return function(target){
target.prototype.element = function(){
return this.$el;
}
}
}
--------------------------------------------------------------------------------------
@element // every item will have this function as Root as Openable
class BaseItem{
y() => number; // we need to get y position of every item
}
OpenableMixin{
render() => render arrow only
open(){} => change arrow position and cause menu to fire change event
close(){} => change arrow position and cause menu to fire change event
}
class ItemsMixin extends OpenableMixing{// for now if item have childs it must be openable,
// but Responsive has only tasks and must be openable too
addItem(item: BaseItem) // need to add generics
removeItem(item: BaseItem)
}
--------------------------------------------------------------------------------------
@Mixin([ItemsMixin, ActivitiesMixin]) // it can have items and activities
class OpenableItem extends BaseItem implement ItemsMixin, ActivitiesMixin { // as in typescript docs
render(){
// call rendering from BaseItem class
super.render();
// call rendering from OpenableMixing
OpenableMixin.prototype.render.call(this);
// do some separate rendering for OpenableItem only
this.$el.append('line');
}
}

@Mixin([ItemsMixin]) // it can have items
class RootItem extends BaseItem implement ItemsMixin{ // and subitems functionality is only for Root class
subitems: Array<BaseItem> // need to add generics to be able to put here different item types
}
--------------------------------------------------------------------------------------
@element
class Menu{
items: Array<Item>
}

@element
class Timeline{
menu: Menu
listAllelement() => {
console.log(this.element());
console.log(this.menu.element());
this.menu.items.forEach((item) => {
console.log(item.element());
if(item.hasChilds()){ // really it must be (item instanceof RootItem || item instanceof OpenableItem)
item.items.forEach((subitem) => { // really we need some recursion here
console.log(subitem.element());
})
}
})
}
}


In reality it is rare situation when you need to implement multiple inheritance, and it is much rarely when you can have such an issue in javascript. But every item can have different functionality depending on needs.



Just imagine that there can be different items which can have number of mixins. Is it wise to put everything into base ? And what is your approach to this issue ?










share|improve this question

























  • Imo you should use DI for this..

    – Dieterg
    Nov 25 '16 at 15:33











  • @LenilsondeCastro Although the idea of multiple inheritance often is dismissed in OOP, it's not really any violation of any principle, it's just that it's not trivial to implement and that we are taught that it's just a stupid idea anyway. In reality, all entities can be categorized in many different unrelated abstractions, and I think it would be neat to have first-class support for that kind of structural organization in a language.

    – Alex
    Nov 25 '16 at 15:47











  • @Dieterg Sorry I'm not sure how DI can help in such situation, but code example is updated.

    – Vayrex
    Dec 10 '16 at 19:14











  • @Alex I was having situation when multiple inheritance was good solution for small python script. And I was not using it in javascript before. But in my case I have a lot of different childs, which need to have separate functionality not only in rendering, but in subscribtions to event emitters and a lot of other things. I was searching for other solutions but mixins seems to be more logical here.

    – Vayrex
    Dec 10 '16 at 19:21














8












8








8


1






I'm need of multiple inheritance in typescript.
Logically it is not good to put a lot of functionality to hierarchy.
I have one base class and number of hierarchy branches.
But I need somehow to use mixings to put some main logic in separate classes, cuz it is not used in every branch.



UPDATE of code exmple:



function Mixin = function(mixins:any){ // mixin decorator
return function(target){
mixins.forEach((mixin) => {
add every function from mixing to target prototype,
if functions with same name does not exists there,
so we are able to do calls to as example different render() functions

Will show it in OpenableItem
});
}
}

function element = function(){
return function(target){
target.prototype.element = function(){
return this.$el;
}
}
}
--------------------------------------------------------------------------------------
@element // every item will have this function as Root as Openable
class BaseItem{
y() => number; // we need to get y position of every item
}
OpenableMixin{
render() => render arrow only
open(){} => change arrow position and cause menu to fire change event
close(){} => change arrow position and cause menu to fire change event
}
class ItemsMixin extends OpenableMixing{// for now if item have childs it must be openable,
// but Responsive has only tasks and must be openable too
addItem(item: BaseItem) // need to add generics
removeItem(item: BaseItem)
}
--------------------------------------------------------------------------------------
@Mixin([ItemsMixin, ActivitiesMixin]) // it can have items and activities
class OpenableItem extends BaseItem implement ItemsMixin, ActivitiesMixin { // as in typescript docs
render(){
// call rendering from BaseItem class
super.render();
// call rendering from OpenableMixing
OpenableMixin.prototype.render.call(this);
// do some separate rendering for OpenableItem only
this.$el.append('line');
}
}

@Mixin([ItemsMixin]) // it can have items
class RootItem extends BaseItem implement ItemsMixin{ // and subitems functionality is only for Root class
subitems: Array<BaseItem> // need to add generics to be able to put here different item types
}
--------------------------------------------------------------------------------------
@element
class Menu{
items: Array<Item>
}

@element
class Timeline{
menu: Menu
listAllelement() => {
console.log(this.element());
console.log(this.menu.element());
this.menu.items.forEach((item) => {
console.log(item.element());
if(item.hasChilds()){ // really it must be (item instanceof RootItem || item instanceof OpenableItem)
item.items.forEach((subitem) => { // really we need some recursion here
console.log(subitem.element());
})
}
})
}
}


In reality it is rare situation when you need to implement multiple inheritance, and it is much rarely when you can have such an issue in javascript. But every item can have different functionality depending on needs.



Just imagine that there can be different items which can have number of mixins. Is it wise to put everything into base ? And what is your approach to this issue ?










share|improve this question
















I'm need of multiple inheritance in typescript.
Logically it is not good to put a lot of functionality to hierarchy.
I have one base class and number of hierarchy branches.
But I need somehow to use mixings to put some main logic in separate classes, cuz it is not used in every branch.



UPDATE of code exmple:



function Mixin = function(mixins:any){ // mixin decorator
return function(target){
mixins.forEach((mixin) => {
add every function from mixing to target prototype,
if functions with same name does not exists there,
so we are able to do calls to as example different render() functions

Will show it in OpenableItem
});
}
}

function element = function(){
return function(target){
target.prototype.element = function(){
return this.$el;
}
}
}
--------------------------------------------------------------------------------------
@element // every item will have this function as Root as Openable
class BaseItem{
y() => number; // we need to get y position of every item
}
OpenableMixin{
render() => render arrow only
open(){} => change arrow position and cause menu to fire change event
close(){} => change arrow position and cause menu to fire change event
}
class ItemsMixin extends OpenableMixing{// for now if item have childs it must be openable,
// but Responsive has only tasks and must be openable too
addItem(item: BaseItem) // need to add generics
removeItem(item: BaseItem)
}
--------------------------------------------------------------------------------------
@Mixin([ItemsMixin, ActivitiesMixin]) // it can have items and activities
class OpenableItem extends BaseItem implement ItemsMixin, ActivitiesMixin { // as in typescript docs
render(){
// call rendering from BaseItem class
super.render();
// call rendering from OpenableMixing
OpenableMixin.prototype.render.call(this);
// do some separate rendering for OpenableItem only
this.$el.append('line');
}
}

@Mixin([ItemsMixin]) // it can have items
class RootItem extends BaseItem implement ItemsMixin{ // and subitems functionality is only for Root class
subitems: Array<BaseItem> // need to add generics to be able to put here different item types
}
--------------------------------------------------------------------------------------
@element
class Menu{
items: Array<Item>
}

@element
class Timeline{
menu: Menu
listAllelement() => {
console.log(this.element());
console.log(this.menu.element());
this.menu.items.forEach((item) => {
console.log(item.element());
if(item.hasChilds()){ // really it must be (item instanceof RootItem || item instanceof OpenableItem)
item.items.forEach((subitem) => { // really we need some recursion here
console.log(subitem.element());
})
}
})
}
}


In reality it is rare situation when you need to implement multiple inheritance, and it is much rarely when you can have such an issue in javascript. But every item can have different functionality depending on needs.



Just imagine that there can be different items which can have number of mixins. Is it wise to put everything into base ? And what is your approach to this issue ?







typescript multiple-inheritance






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 10 '16 at 19:07







Vayrex

















asked Nov 25 '16 at 15:04









VayrexVayrex

6791511




6791511













  • Imo you should use DI for this..

    – Dieterg
    Nov 25 '16 at 15:33











  • @LenilsondeCastro Although the idea of multiple inheritance often is dismissed in OOP, it's not really any violation of any principle, it's just that it's not trivial to implement and that we are taught that it's just a stupid idea anyway. In reality, all entities can be categorized in many different unrelated abstractions, and I think it would be neat to have first-class support for that kind of structural organization in a language.

    – Alex
    Nov 25 '16 at 15:47











  • @Dieterg Sorry I'm not sure how DI can help in such situation, but code example is updated.

    – Vayrex
    Dec 10 '16 at 19:14











  • @Alex I was having situation when multiple inheritance was good solution for small python script. And I was not using it in javascript before. But in my case I have a lot of different childs, which need to have separate functionality not only in rendering, but in subscribtions to event emitters and a lot of other things. I was searching for other solutions but mixins seems to be more logical here.

    – Vayrex
    Dec 10 '16 at 19:21



















  • Imo you should use DI for this..

    – Dieterg
    Nov 25 '16 at 15:33











  • @LenilsondeCastro Although the idea of multiple inheritance often is dismissed in OOP, it's not really any violation of any principle, it's just that it's not trivial to implement and that we are taught that it's just a stupid idea anyway. In reality, all entities can be categorized in many different unrelated abstractions, and I think it would be neat to have first-class support for that kind of structural organization in a language.

    – Alex
    Nov 25 '16 at 15:47











  • @Dieterg Sorry I'm not sure how DI can help in such situation, but code example is updated.

    – Vayrex
    Dec 10 '16 at 19:14











  • @Alex I was having situation when multiple inheritance was good solution for small python script. And I was not using it in javascript before. But in my case I have a lot of different childs, which need to have separate functionality not only in rendering, but in subscribtions to event emitters and a lot of other things. I was searching for other solutions but mixins seems to be more logical here.

    – Vayrex
    Dec 10 '16 at 19:21

















Imo you should use DI for this..

– Dieterg
Nov 25 '16 at 15:33





Imo you should use DI for this..

– Dieterg
Nov 25 '16 at 15:33













@LenilsondeCastro Although the idea of multiple inheritance often is dismissed in OOP, it's not really any violation of any principle, it's just that it's not trivial to implement and that we are taught that it's just a stupid idea anyway. In reality, all entities can be categorized in many different unrelated abstractions, and I think it would be neat to have first-class support for that kind of structural organization in a language.

– Alex
Nov 25 '16 at 15:47





@LenilsondeCastro Although the idea of multiple inheritance often is dismissed in OOP, it's not really any violation of any principle, it's just that it's not trivial to implement and that we are taught that it's just a stupid idea anyway. In reality, all entities can be categorized in many different unrelated abstractions, and I think it would be neat to have first-class support for that kind of structural organization in a language.

– Alex
Nov 25 '16 at 15:47













@Dieterg Sorry I'm not sure how DI can help in such situation, but code example is updated.

– Vayrex
Dec 10 '16 at 19:14





@Dieterg Sorry I'm not sure how DI can help in such situation, but code example is updated.

– Vayrex
Dec 10 '16 at 19:14













@Alex I was having situation when multiple inheritance was good solution for small python script. And I was not using it in javascript before. But in my case I have a lot of different childs, which need to have separate functionality not only in rendering, but in subscribtions to event emitters and a lot of other things. I was searching for other solutions but mixins seems to be more logical here.

– Vayrex
Dec 10 '16 at 19:21





@Alex I was having situation when multiple inheritance was good solution for small python script. And I was not using it in javascript before. But in my case I have a lot of different childs, which need to have separate functionality not only in rendering, but in subscribtions to event emitters and a lot of other things. I was searching for other solutions but mixins seems to be more logical here.

– Vayrex
Dec 10 '16 at 19:21












3 Answers
3






active

oldest

votes


















9














Currently, TypeScript does not have any special syntax to express multiple inheritance or mixins, but the official workaround can be found here.



That strategy is basically that you let your main class implement the classes that you want, write simple dummy implementation for their interfaces, and then have a special function that reads your classes and overwrites the properties in your main class.



There are some suggestions to make TypeScript better at mixins/traits, eg. #2919 and #311.






share|improve this answer


























  • Thank you. But I came here after reading of that posts. I have done update of code example and now main question is changed.

    – Vayrex
    Dec 10 '16 at 19:12



















4














@kjetil-klaussen is correct, typescript 2.2 added support for the ECMAscript 2017 mixin pattern



Here is a full code sample:



// http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/
// https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#support-for-mix-in-classes

type Constructor<T> = new (...args: any) => T;

class S {
foo() {
console.log('foo from S');
}
}

// Here the possible SuperClass is set to {} (Object)
function Mixin1<T extends Constructor<{}>>(SuperClass: T) {
return class extends SuperClass {
foo() {
console.log('foo from Mixin1');
if (super.foo) super.foo();
}
};
}

// Here the possible SuperClass (S) is specified
function Mixin2<T extends Constructor<S>>(SuperClass: T) {
return class extends SuperClass {
foo() {
console.log('foo from Mixin2');
super.foo();
}
};
}

class C extends Mixin1(Mixin2(S)) {
foo() {
console.log('foo from C');
super.foo();
}
}

new C().foo();


This outputs:



foo from C
foo from Mixin1
foo from Mixin2
foo from S





share|improve this answer

































    3














    Typescript 2.2 added support for mix-ins.






    share|improve this answer





















    • 24





      And how would this user use this new mixin functionality?

      – Seanny123
      Aug 2 '17 at 2:25











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40807808%2ftypescript-multiple-inheritance%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    9














    Currently, TypeScript does not have any special syntax to express multiple inheritance or mixins, but the official workaround can be found here.



    That strategy is basically that you let your main class implement the classes that you want, write simple dummy implementation for their interfaces, and then have a special function that reads your classes and overwrites the properties in your main class.



    There are some suggestions to make TypeScript better at mixins/traits, eg. #2919 and #311.






    share|improve this answer


























    • Thank you. But I came here after reading of that posts. I have done update of code example and now main question is changed.

      – Vayrex
      Dec 10 '16 at 19:12
















    9














    Currently, TypeScript does not have any special syntax to express multiple inheritance or mixins, but the official workaround can be found here.



    That strategy is basically that you let your main class implement the classes that you want, write simple dummy implementation for their interfaces, and then have a special function that reads your classes and overwrites the properties in your main class.



    There are some suggestions to make TypeScript better at mixins/traits, eg. #2919 and #311.






    share|improve this answer


























    • Thank you. But I came here after reading of that posts. I have done update of code example and now main question is changed.

      – Vayrex
      Dec 10 '16 at 19:12














    9












    9








    9







    Currently, TypeScript does not have any special syntax to express multiple inheritance or mixins, but the official workaround can be found here.



    That strategy is basically that you let your main class implement the classes that you want, write simple dummy implementation for their interfaces, and then have a special function that reads your classes and overwrites the properties in your main class.



    There are some suggestions to make TypeScript better at mixins/traits, eg. #2919 and #311.






    share|improve this answer















    Currently, TypeScript does not have any special syntax to express multiple inheritance or mixins, but the official workaround can be found here.



    That strategy is basically that you let your main class implement the classes that you want, write simple dummy implementation for their interfaces, and then have a special function that reads your classes and overwrites the properties in your main class.



    There are some suggestions to make TypeScript better at mixins/traits, eg. #2919 and #311.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 25 '16 at 15:41

























    answered Nov 25 '16 at 15:34









    AlexAlex

    8,08453964




    8,08453964













    • Thank you. But I came here after reading of that posts. I have done update of code example and now main question is changed.

      – Vayrex
      Dec 10 '16 at 19:12



















    • Thank you. But I came here after reading of that posts. I have done update of code example and now main question is changed.

      – Vayrex
      Dec 10 '16 at 19:12

















    Thank you. But I came here after reading of that posts. I have done update of code example and now main question is changed.

    – Vayrex
    Dec 10 '16 at 19:12





    Thank you. But I came here after reading of that posts. I have done update of code example and now main question is changed.

    – Vayrex
    Dec 10 '16 at 19:12













    4














    @kjetil-klaussen is correct, typescript 2.2 added support for the ECMAscript 2017 mixin pattern



    Here is a full code sample:



    // http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/
    // https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#support-for-mix-in-classes

    type Constructor<T> = new (...args: any) => T;

    class S {
    foo() {
    console.log('foo from S');
    }
    }

    // Here the possible SuperClass is set to {} (Object)
    function Mixin1<T extends Constructor<{}>>(SuperClass: T) {
    return class extends SuperClass {
    foo() {
    console.log('foo from Mixin1');
    if (super.foo) super.foo();
    }
    };
    }

    // Here the possible SuperClass (S) is specified
    function Mixin2<T extends Constructor<S>>(SuperClass: T) {
    return class extends SuperClass {
    foo() {
    console.log('foo from Mixin2');
    super.foo();
    }
    };
    }

    class C extends Mixin1(Mixin2(S)) {
    foo() {
    console.log('foo from C');
    super.foo();
    }
    }

    new C().foo();


    This outputs:



    foo from C
    foo from Mixin1
    foo from Mixin2
    foo from S





    share|improve this answer






























      4














      @kjetil-klaussen is correct, typescript 2.2 added support for the ECMAscript 2017 mixin pattern



      Here is a full code sample:



      // http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/
      // https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#support-for-mix-in-classes

      type Constructor<T> = new (...args: any) => T;

      class S {
      foo() {
      console.log('foo from S');
      }
      }

      // Here the possible SuperClass is set to {} (Object)
      function Mixin1<T extends Constructor<{}>>(SuperClass: T) {
      return class extends SuperClass {
      foo() {
      console.log('foo from Mixin1');
      if (super.foo) super.foo();
      }
      };
      }

      // Here the possible SuperClass (S) is specified
      function Mixin2<T extends Constructor<S>>(SuperClass: T) {
      return class extends SuperClass {
      foo() {
      console.log('foo from Mixin2');
      super.foo();
      }
      };
      }

      class C extends Mixin1(Mixin2(S)) {
      foo() {
      console.log('foo from C');
      super.foo();
      }
      }

      new C().foo();


      This outputs:



      foo from C
      foo from Mixin1
      foo from Mixin2
      foo from S





      share|improve this answer




























        4












        4








        4







        @kjetil-klaussen is correct, typescript 2.2 added support for the ECMAscript 2017 mixin pattern



        Here is a full code sample:



        // http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/
        // https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#support-for-mix-in-classes

        type Constructor<T> = new (...args: any) => T;

        class S {
        foo() {
        console.log('foo from S');
        }
        }

        // Here the possible SuperClass is set to {} (Object)
        function Mixin1<T extends Constructor<{}>>(SuperClass: T) {
        return class extends SuperClass {
        foo() {
        console.log('foo from Mixin1');
        if (super.foo) super.foo();
        }
        };
        }

        // Here the possible SuperClass (S) is specified
        function Mixin2<T extends Constructor<S>>(SuperClass: T) {
        return class extends SuperClass {
        foo() {
        console.log('foo from Mixin2');
        super.foo();
        }
        };
        }

        class C extends Mixin1(Mixin2(S)) {
        foo() {
        console.log('foo from C');
        super.foo();
        }
        }

        new C().foo();


        This outputs:



        foo from C
        foo from Mixin1
        foo from Mixin2
        foo from S





        share|improve this answer















        @kjetil-klaussen is correct, typescript 2.2 added support for the ECMAscript 2017 mixin pattern



        Here is a full code sample:



        // http://justinfagnani.com/2015/12/21/real-mixins-with-javascript-classes/
        // https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#support-for-mix-in-classes

        type Constructor<T> = new (...args: any) => T;

        class S {
        foo() {
        console.log('foo from S');
        }
        }

        // Here the possible SuperClass is set to {} (Object)
        function Mixin1<T extends Constructor<{}>>(SuperClass: T) {
        return class extends SuperClass {
        foo() {
        console.log('foo from Mixin1');
        if (super.foo) super.foo();
        }
        };
        }

        // Here the possible SuperClass (S) is specified
        function Mixin2<T extends Constructor<S>>(SuperClass: T) {
        return class extends SuperClass {
        foo() {
        console.log('foo from Mixin2');
        super.foo();
        }
        };
        }

        class C extends Mixin1(Mixin2(S)) {
        foo() {
        console.log('foo from C');
        super.foo();
        }
        }

        new C().foo();


        This outputs:



        foo from C
        foo from Mixin1
        foo from Mixin2
        foo from S






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 26 '18 at 13:45

























        answered Nov 14 '18 at 9:10









        Simon EpskampSimon Epskamp

        4,56613340




        4,56613340























            3














            Typescript 2.2 added support for mix-ins.






            share|improve this answer





















            • 24





              And how would this user use this new mixin functionality?

              – Seanny123
              Aug 2 '17 at 2:25
















            3














            Typescript 2.2 added support for mix-ins.






            share|improve this answer





















            • 24





              And how would this user use this new mixin functionality?

              – Seanny123
              Aug 2 '17 at 2:25














            3












            3








            3







            Typescript 2.2 added support for mix-ins.






            share|improve this answer















            Typescript 2.2 added support for mix-ins.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 15 '17 at 0:54









            Dave Cousineau

            7,34554058




            7,34554058










            answered May 6 '17 at 5:15









            Kjetil KlaussenKjetil Klaussen

            4,40712929




            4,40712929








            • 24





              And how would this user use this new mixin functionality?

              – Seanny123
              Aug 2 '17 at 2:25














            • 24





              And how would this user use this new mixin functionality?

              – Seanny123
              Aug 2 '17 at 2:25








            24




            24





            And how would this user use this new mixin functionality?

            – Seanny123
            Aug 2 '17 at 2:25





            And how would this user use this new mixin functionality?

            – Seanny123
            Aug 2 '17 at 2:25


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f40807808%2ftypescript-multiple-inheritance%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks