{"id":4116,"date":"2016-05-14T02:53:16","date_gmt":"2016-05-14T02:53:16","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4116"},"modified":"2016-05-14T02:53:16","modified_gmt":"2016-05-14T02:53:16","slug":"typescript-array-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/typescript-array-example\/","title":{"rendered":"Typescript Array Example"},"content":{"rendered":"<p>You can create arrays in Typescript quite simply:<\/p>\n<pre lang=\"javascript\">\nlet range: number[];\nrange = [1, 2];\n<\/pre>\n<p>You can do some quite novel things as well, such as to set the object to a range of types:<\/p>\n<pre lang=\"javascript\">\nlet range: number[] | string[];\nrange = [1, 2];\nrange = ['a', 'b'];\n\nfor (let i = 0; i < range.length; i++) {\n  console.log(range[i].toString());\n}\n<\/pre>\n<p>If you want to \"cast\" the value, you can also do it with an if statement around the item:<\/p>\n<pre lang=\"javascript\">\nfor (let i = 0; i < range.length; i++) {\n  let listVal = range[i];\n  if (typeof listVal === \"number\") {\n    console.log(listVal + 1);\n  }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Examples that show how arrays 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\/4116"}],"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=4116"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4116\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}