{"id":4524,"date":"2016-06-18T02:27:27","date_gmt":"2016-06-18T02:27:27","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4524"},"modified":"2016-06-18T02:27:27","modified_gmt":"2016-06-18T02:27:27","slug":"typescript-enum-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/typescript-enum-example\/","title":{"rendered":"TypeScript enum example"},"content":{"rendered":"<p>TypeScript enums work basically how&#8217;d you&#8217;d expect from any normal language:<\/p>\n<pre lang=\"TypeScript\">\nenum LogLevel {\n  info, debug, error, critical, none\n}<\/pre>\n<p>These are numbered sequentially, so you can use them like so:<\/p>\n<pre lang=\"javascript\">\nif (level >= currentLevel) {\n  onLog(message);\n}\n<\/pre>\n<p>You can also set the numbers, as you&#8217;d expect:<\/p>\n<pre lang=\"TypeScript\">\nenum LogLevel {\n  info = 7, debug, error, critical, none\n}<\/pre>\n<p>If you do this, the subsequent numbers will be 8, 9, etc.<\/p>\n<p>Some documentation I&#8217;ve seen<sup><a href=\"#footnote_0_4524\" id=\"identifier_0_4524\" class=\"footnote-link footnote-identifier-link\" title=\"https:\/\/basarat.gitbooks.io\/typescript\/content\/docs\/enums.html\">1<\/a><\/sup> also recommends the use of these as flags, which is pretty neat as well (you see this a lot in code generated from other languages &#8211; e.g. ScalaJS or enscripten).<\/p>\n<pre lang=\"javascript\">\nenum AnimalFlags {\n    None           = 0,\n    HasClaws       = 1 << 0,\n    CanFly         = 1 << 1,\n    EatsFish       = 1 << 2,\n    Endangered     = 1 << 3\n}\n<\/pre>\n<ol class=\"footnotes\"><li id=\"footnote_0_4524\" class=\"footnote\">https:\/\/basarat.gitbooks.io\/typescript\/content\/docs\/enums.html<span class=\"footnote-back-link-wrapper\"> [<a href=\"#identifier_0_4524\" class=\"footnote-link footnote-back-link\">&#8617;<\/a>]<\/span><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>Enum examples 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\/4524"}],"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=4524"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4524\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}