{"id":138,"date":"2020-11-17T12:38:32","date_gmt":"2020-11-17T11:38:32","guid":{"rendered":"https:\/\/info.edython.eu\/?p=138"},"modified":"2020-11-17T12:38:32","modified_gmt":"2020-11-17T11:38:32","slug":"subclassing-map-with-mixins","status":"publish","type":"post","link":"https:\/\/info.edython.eu\/index.php\/2020\/11\/17\/subclassing-map-with-mixins\/","title":{"rendered":"Subclassing Map with mixins"},"content":{"rendered":"\n<p>The problem arises because <code>Map<\/code> is defined with generics.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.typescriptlang.org\/play?#code\/C4TwDgpgBAwg9gOwM7AE4FcDGw6qgXigQgHcoAKAOmoENUBzJALihoRAG0BdASgID4oAbwC+AKABm6BNgCWiKBAAewCMnkIAPABUAQjSTRlqhABMksRCgzZc-cvsMs9BiHyFioUVBGDpUCFCYADYGFsZq5lCO0B5eXpg+NMAaAPqmydCEACKZlAhwJOQ8nlDi4iFhUABicHCKKpHhjeqI5JVIFqLuFVZwwRCUwXD05MRktXDFlIkQyWkZqiWS0nIKALY0YACiLUgamgDSADRQAGr27qU+fgENJvttHc0mUQCyW0enF91i4mKYKzAKAfMAAOXQ6wARhA8IRNjs9gdrLIEPRTghITDUJcAUCoOtAoRxiCthDobDinjkP1BsNRoSZkkUoh0pkSozDMByAByCR1HmnACMACYAMwlQE0gZDEbkRn0Xy8-lwHk8ZZSlCksAAZTQqPoBAJW12D2R+rRpxRaNxmuB6wsxNI2r1qANVM1tNlDKQTLmLIQbKWYgdlC5yoFpx5ULoaupSC99PlvsV3L5AvVYiAA\">TS playground<\/a><\/p>\n\n\n\n<h2>Given a mixins extension<\/h2>\n\n\n\n<p>This extension just adds a <code>creation_date<\/code> attribute to any instance.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">type Constructor = new (...args: any[]) => {}\nfunction extension&lt;TBase extends Constructor>(Base: TBase) {\n  return class extends Base {\n    creation_date = Date.now()\n  }\n}\nclass Foo extends extension(class {}) {}\nconsole.log(new Foo().creation_date)<\/code><\/pre>\n\n\n\n<h2>Extending Map<\/h2>\n\n\n\n<p>What we would like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">...\nconst mn = new XMap&lt;string, number>()\nconsole.log(mn.creation_date)\nmn.set('foo', 123)\nconsole.log(mn.get('foo'))\n\nconst ms = new XMap&lt;string, string>()\nconsole.log(ms.creation_date)\nms.set('foo', 'bar')\nconsole.log(ms.get('foo'))\n\nclass Foo&lt;K, V> {\n  map?: Map&lt;K, V>\n}<\/code><\/pre>\n\n\n\n<p>Actually, we have<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">function mapExtension&lt;K, V>() {\n  return extension(class extends Map&lt;K, V>{})\n}\n\nconst MapNumber = mapExtension&lt;string, number>()\nconst mn = new MapNumber()\nconsole.log(mn.creation_date)\nmn.set('foo', 123)\nconsole.log(mn.get('foo'))\n\nconst MapString = mapExtension&lt;string, string>()\nconst ms = new MapString()\nconsole.log(ms.creation_date)\nms.set('foo', 'bar')\nconsole.log(ms.get('foo'))<\/code><\/pre>\n\n\n\n<p>This is too wordy, although tolerable. But the most important thing is that we lost the <code>Map<\/code> generics.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The problem arises because Map is defined with generics. TS playground Given a mixins extension This extension just adds a creation_date attribute to any instance. Extending Map What we would like: Actually, we have This is too wordy, although tolerable. But the most important thing is that we lost the Map generics.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/posts\/138"}],"collection":[{"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/comments?post=138"}],"version-history":[{"count":2,"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/posts\/138\/revisions\/140"}],"wp:attachment":[{"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}