{"id":3742,"date":"2016-04-16T02:21:51","date_gmt":"2016-04-16T02:21:51","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3742"},"modified":"2016-04-16T02:21:51","modified_gmt":"2016-04-16T02:21:51","slug":"typescript-interface-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/typescript-interface-example\/","title":{"rendered":"TypeScript interface example"},"content":{"rendered":"<p>TypeScript has a structure called interfaces, although they look like a struct and act like a duck type.<\/p>\n<p>Observe (note you can put semicolons after the value declarations if you like):<\/p>\n<pre lang=\"javascript\">\ninterface Model {\n    title: string\n    contents: string\n}\n<\/pre>\n<p>When using this, we can reference the values in code (and, Visual Studio code can do Intellisense on these)<\/p>\n<pre lang=\"javascript\">\nclass Dialog {\n    render(data: Model) {\n        console.log(data.contents)\n    }\n}\n\nlet dialog: Dialog = new Dialog()\n<\/pre>\n<p>However, you don&#8217;t have to instantiate the type &#8211; the keys just have to have the right names, so they are effectively duck typed.<\/p>\n<pre lang=\"javascript\">\ndialog.render({\n    title: \"test title\",\n    contents: \"contents\"\n}); \n<\/pre>\n<p>If you wanted to declare a variable of type Model, you can also do this:<\/p>\n<pre lang=\"javascript\">\nlet x: Model = {\n    title: \"test\",\n    contents: \"test contents\"\n };\n<\/pre>\n<p>Note that you can&#8217;t instantiate the interface like you might in Java \/ Scala, or you&#8217;ll get this error:<\/p>\n<pre>\ntest.ts(13,29): error TS1005: ',' expected.\n<\/pre>\n<p>Finally, the type checks only exist at compile time, resembling type erasure in Java. That said, this is incredible compared to Javascript.<\/p>\n<p>The final compiled result of this shows the type erasure, like so:<\/p>\n<pre lang=\"javascript\">\nvar Dialog = (function () {\n    function Dialog() {\n    }\n    Dialog.prototype.render = function (data) {\n        console.log(data.title);\n    };\n    return Dialog;\n}());\nvar x = {\n    title: '123',\n    contents: 'aaa'\n};\nvar d = new Dialog();\nd.render(x);\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An example of how interfaces work in TypeScript, compared to more mainstream languages<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302,557],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3742"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3742"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3742\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}