{"id":143,"date":"2020-11-20T11:34:48","date_gmt":"2020-11-20T10:34:48","guid":{"rendered":"https:\/\/info.edython.eu\/?p=143"},"modified":"2020-11-20T11:36:27","modified_gmt":"2020-11-20T10:36:27","slug":"parent-child-paradigm","status":"publish","type":"post","link":"https:\/\/info.edython.eu\/index.php\/2020\/11\/20\/parent-child-paradigm\/","title":{"rendered":"Parent child paradigm"},"content":{"rendered":"\n<p>A parent may have a child, a child may have a parent, but no more than one. Is it possible to make the difference using types ?<\/p>\n\n\n\n<p>Here are the classes<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">class Child &lt;T extends Parent | undefined> {\n  parent?: T\n}\nclass Parent {\n  child?: Child&lt;this>\n  change($: Child&lt;undefined>): Child&lt;undefined> | undefined {\n    const ans = this.child\n    const $$ = $ as unknown as Child&lt;this>\n    $$.parent = this\n    this.child = $$\n    return ans as unknown as Child&lt;undefined>\n  }\n}<\/code><\/pre>\n\n\n\n<p>And usage:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"typescript\" class=\"language-typescript\">const parent1 = new Parent()\nconst parent2 = new Parent()\nparent1.change(new Child())\nparent2.change(parent1.child)<\/code><\/pre>\n\n\n\n<p>Last line expectedly produces an error as <code>parent1.child<\/code> already has a parent!<\/p>\n\n\n\n<p>There is a minor problem however: the extra variable <code>$$<\/code> was used to avoid typecasting multiple times. We hope this is optimized by the transpiler\/minifier used before shipping. Actually <code>tsc<\/code> does not optimize that code.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A parent may have a child, a child may have a parent, but no more than one. Is it possible to make the difference using types ? Here are the classes And usage: Last line expectedly produces an error as parent1.child already has a parent! There is a minor problem however: the extra variable $$ &hellip; <a href=\"https:\/\/info.edython.eu\/index.php\/2020\/11\/20\/parent-child-paradigm\/\" class=\"more-link\">Continuer la lecture<span class=\"screen-reader-text\"> de &laquo;&nbsp;Parent child paradigm&nbsp;&raquo;<\/span><\/a><\/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\/143"}],"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=143"}],"version-history":[{"count":3,"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":146,"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/posts\/143\/revisions\/146"}],"wp:attachment":[{"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/info.edython.eu\/index.php\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}