{"id":1624,"date":"2022-01-28T18:07:17","date_gmt":"2022-01-28T17:07:17","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1624"},"modified":"2022-01-28T18:22:37","modified_gmt":"2022-01-28T17:22:37","slug":"typescript-multiple-inheritance-issue","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/01\/28\/typescript-multiple-inheritance-issue\/","title":{"rendered":"TypeScript &#038;  Inheritance"},"content":{"rendered":"\n<p><span class=\"has-inline-color has-vivid-red-color\">Inheritance <\/span>is an aspect of OOPs languages, which provides the ability of a program to create a new class from an existing class. It is a mechanism which acquires the\u00a0<strong>properties<\/strong>\u00a0and\u00a0<strong>behaviors<\/strong>\u00a0of a class from another class. The class whose members are inherited is called the\u00a0<strong>base class<\/strong>, and the class that inherits those members is called the\u00a0<strong>derived\/child\/subclass<\/strong>. In child class, we can override or modify the behaviors of its parent class.<\/p>\n\n\n\n<p>Before ES6 (ES2015) , JavaScript uses\u00a0<strong>functions<\/strong>\u00a0and\u00a0<strong>prototype-based<\/strong>\u00a0inheritance, but TypeScript supports the\u00a0<strong>class-based<\/strong>\u00a0inheritance which comes from ES6 version. The TypeScript uses class inheritance through the\u00a0<strong>extends<\/strong>\u00a0keyword.\u00a0<\/p>\n\n\n\n<p><em><span class=\"has-inline-color has-vivid-red-color\">PS:<\/span><strong>TypeScript does not support multiple inheritance<\/strong>.<\/em><\/p>\n\n\n\n<p>\u00a0TypeScript supports only\u00a0<strong>single<\/strong>\u00a0inheritance and\u00a0<strong>multilevel<\/strong>\u00a0inheritance.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4>Types of Inheritance<\/h4>\n\n\n\n<p>We can classify the inheritance into the five types. These are:<\/p>\n\n\n\n<ul><li>Single Inheritance<\/li><li>Multilevel Inheritance<\/li><li>Multiple Inheritance<\/li><li>Hierarchical Inheritance<\/li><li>Hybrid Inheritance<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"799\" height=\"559\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/01\/image-24.png\" alt=\"\" class=\"wp-image-1627\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/01\/image-24.png 799w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/01\/image-24-300x210.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/01\/image-24-768x537.png 768w\" sizes=\"(max-width: 799px) 100vw, 799px\" \/><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>How can we do, if we face a <em>&#8220;multiple inheritance&#8221;<\/em> issue in typescript ? <\/h4>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-green-cyan-color\">Workaround 1 :<\/span><\/h4>\n\n\n\n<p>first tip: whenever we come across a multiple inheritance problem, let\u00b4s think <strong><em>&#8220;multiple level inheritance over multiple inheritance&#8221;<\/em><\/strong> to see if that&#8217;ll do the job:<\/p>\n\n\n\n<p><em>Class B extends A, Class C extendes B, <\/em><span class=\"has-inline-color has-vivid-red-color\"><em>and so on ..<\/em>. <\/span><\/p>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-green-cyan-color\">Workaround 2<\/span><\/h4>\n\n\n\n<p>There is a feature in TypeScript that allows you to use <em><strong>Mixins<\/strong><\/em> to create re-usable small objects. You can compose these into larger objects using multiple inheritance <em><strong>(multiple inheritance is not allowed for classes, but it is allowed for mixins<\/strong><\/em> &#8211; which are like interfaces with an associated implenentation).<\/p>\n\n\n\n<p>Here is a quick Mixins demo&#8230; first, the flavours that you want to mix:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class CanEat {\r\n    public eat() {\r\n        alert('Munch Munch.');\r\n    }\r\n}\r\n\r\nclass CanSleep {\r\n    sleep() {\r\n        alert('Zzzzzzz.');\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>Then the magic method for Mixin creation (you only need this once somewhere in your program&#8230;)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function applyMixins(derivedCtor: any, baseCtors: any&#91;]) {\r\n    baseCtors.forEach(baseCtor => {\r\n        Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {\r\n             if (name !== 'constructor') {\r\n                derivedCtor.prototype&#91;name] = baseCtor.prototype&#91;name];\r\n            }\r\n        });\r\n    }); \r\n}<\/code><\/pre>\n\n\n\n<p>And then you can create classes with multiple inheritance from mixin flavours:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Being implements CanEat, CanSleep {\r\n        eat: () => void;\r\n        sleep: () => void;\r\n}\r\napplyMixins (Being, &#91;CanEat, CanSleep]);<\/code><\/pre>\n\n\n\n<p>Note that there is no actual implementation in this class &#8211; just enough to make it pass the requirements of the &#8220;interfaces&#8221;. But when we use this class &#8211; it all works.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var being = new Being();\r\n\r\n\/\/ Zzzzzzz...\r\nbeing.sleep();<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance is an aspect of OOPs languages, which provides the ability of a program to create a new class from an existing class. It is a mechanism which acquires the\u00a0properties\u00a0and\u00a0behaviors\u00a0of a class from another class. The class whose members are inherited is called the\u00a0base class, and the class that inherits those members is called the\u00a0derived\/child\/subclass. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[83],"tags":[393,473,474,471,472],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1624"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1624"}],"version-history":[{"count":3,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1624\/revisions"}],"predecessor-version":[{"id":1628,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1624\/revisions\/1628"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1624"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1624"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1624"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}