{"id":4118,"date":"2016-05-14T02:55:18","date_gmt":"2016-05-14T02:55:18","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4118"},"modified":"2016-05-14T02:55:18","modified_gmt":"2016-05-14T02:55:18","slug":"typescript-union-type-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/typescript-union-type-example\/","title":{"rendered":"Typescript: union type example"},"content":{"rendered":"<p>In Typescript, a type can be one of a list of types. This is a pretty novel feature that is necessary to support Javascript &#8211; I can&#8217;t think of any mainstream languages that support something like this. <\/p>\n<p>For instance, the following code would be valid:<\/p>\n<pre lang=\"javascript\">\nlet x : string | number;\n\nx = 1;\nx = '1';\n<\/pre>\n<p>Anything that is common (assume duck-typing) can be called on the item:<\/p>\n<pre lang=\"javascript\">\nconsole.log(x.toString());\n<\/pre>\n<p>If you use non-primitive types, you can use instanceof, like you would in Java or C#:<\/p>\n<pre lang=\"javascript\">\nif (x instanceof String) {\n  ...      \n}\n<\/pre>\n<p>If you want to use primitives, you have to use the typeof from Javascript:<\/p>\n<pre lang=\"javascript\">\nif (typeof x === \"number\") {\n  ...\n}\n\nif (typeof x === \"string\") {\n  ...     \n}\n<\/pre>\n<p>While this looks hokey, it is quite magical &#8211; the use of &#8220;if&#8221; around the code in question causes an implicit cast:<\/p>\n<pre lang=\"javascript\">\nif (typeof listVal === \"number\") {\n  console.log(listVal + 1);\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Examples of how union types work in Typescript<\/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\/4118"}],"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=4118"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4118\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}