{"id":1477,"date":"2024-12-30T23:33:51","date_gmt":"2024-12-30T23:33:51","guid":{"rendered":"https:\/\/kalkus.dev\/blog\/?p=1477"},"modified":"2024-12-30T23:33:51","modified_gmt":"2024-12-30T23:33:51","slug":"how-to-define-enum-type-in-typescript","status":"publish","type":"post","link":"https:\/\/kalkus.dev\/blog\/2024\/12\/30\/how-to-define-enum-type-in-typescript\/","title":{"rendered":"How to define enum type in Typescript"},"content":{"rendered":"\n<p>My favorite way of defining types for enums in TypeScript is simple yet powerful, though slightly counterintuitive. First, we define a <code>const<\/code> with keys and values, then construct a type from it.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"typescript\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const Animal = {\n   Dog: 'dog',\n   Cat: 'cat'\n} as const;\n\ntype Animal = typeof Animal[keyof typeof Animal];\n<\/pre>\n\n\n\n<p>It may feel counterintuitive because we create both a <code>const<\/code> and a <code>type<\/code> with the same name, but it works seamlessly.<br>We can conveniently use the type like an enum!<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (someAnimal === Animal.Dog) {\n   ...\n}\n<\/pre>\n\n\n\n<p>This approach offers autocompletion benefits, aligns with functional programming principles, and has zero runtime overhead (unlike enums).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My favorite way of defining types for enums in TypeScript is simple yet powerful, though slightly counterintuitive. First, we define a const with keys and values, then construct a type from it. It may feel counterintuitive because we create both a const and a type with the same name, but it works seamlessly.We can conveniently [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[217],"_links":{"self":[{"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/posts\/1477"}],"collection":[{"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/comments?post=1477"}],"version-history":[{"count":1,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/posts\/1477\/revisions"}],"predecessor-version":[{"id":1480,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/posts\/1477\/revisions\/1480"}],"wp:attachment":[{"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/media?parent=1477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/categories?post=1477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kalkus.dev\/blog\/wp-json\/wp\/v2\/tags?post=1477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}