{"id":1609,"date":"2021-12-03T15:20:06","date_gmt":"2021-12-03T06:20:06","guid":{"rendered":"https:\/\/weseek.co.jp\/tech\/?p=1609"},"modified":"2023-04-17T11:15:57","modified_gmt":"2023-04-17T02:15:57","slug":"%e4%bf%9d%e5%ad%98%e7%89%88-typescript%e3%81%ae-enum%e5%9e%8b-%e3%81%ae%e4%bb%a3%e3%82%8f%e3%82%8a%e3%81%ab-union%e5%9e%8b%e3%82%92%e4%bd%bf%e7%94%a8%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/weseek.co.jp\/tech\/1609\/","title":{"rendered":"Typescript\u306eEnum\u578b\u306e\u4ee3\u308f\u308a\u306bUnion\u578b\u3092\u4f7f\u7528\u3059\u308b"},"content":{"rendered":"<p>\u3053\u306e\u6295\u7a3f\u306f\u3001\u5f0a\u793e\u304c\u63d0\u4f9b\u3059\u308bWESEEK TECH\u901a\u4fe1\u306e\u4e00\u74b0\u3067\u3059\u3002<br \/>\nWESEEK TECH\u901a\u4fe1\u3068\u306f\u3001WESEEK\u306e\u30a8\u30f3\u30b8\u30cb\u30a2\u304c\u30ad\u30e3\u30c3\u30c1\u30a2\u30c3\u30d7\u3057\u305f\u6280\u8853\u306b\u95a2\u3059\u308b\u60c5\u5831\u3092\u3001tech\u30d6\u30ed\u30b0\u3092\u901a\u3058\u3066\u5b9a\u671f\u7684\u306b\u767a\u4fe1\u3057\u3066\u3044\u304f\u3082\u306e\u3067\u3059\u3002<\/p>\n<h2>\u306f\u3058\u3081\u306b<\/h2>\n<p>\u3053\u3093\u306b\u3061\u306f\u3001\u30b7\u30b9\u30c6\u30e0\u30a8\u30f3\u30b8\u30cb\u30a2\u306e <a href=\"https:\/\/github.com\/kaoritokashiki\">\u304b\u304a\u308a<\/a> \u3067\u3059\u3002\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001TypeScript\u3067\u7528\u3044\u3089\u308c\u308b Enum\u578b \u3068Union\u578b\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u304b\u3089\u3001Enum\u3092\u907f\u3051\u308b\u3079\u304d\u3068\u8a00\u308f\u308c\u3066\u3044\u308b\u7406\u7531\u3001Union\u578b\u3092\u7528\u3044\u3066Enum\u306e\u3088\u3046\u306b\u66f8\u304f\u65b9\u6cd5\u306a\u3069\u3092\u8aac\u660e\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n<p><!--more--><\/p>\n<h2>Enum (\u5217\u6319\u578b) \u3068\u306f?<\/h2>\n<p>\u305d\u3082\u305d\u3082 Enum \u3068\u306f\u4f55\u304b\u3001\u306a\u3093\u306e\u305f\u3081\u306b\u4f7f\u3046\u3082\u306e\u306a\u306e\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u3053\u3053\u3067\u8aac\u660e\u3057\u3066\u3044\u308b Enum \u3068\u306f\u7dcf\u79f0\u3067\u3042\u308a\u3001\u5217\u6319\u578b\u3068\u3082\u547c\u3070\u308c\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u4f8b\u306f TypeScript \u306e\u30b3\u30fc\u30c9\u3067\u3059\u304c\u3001\u4ed6\u306e\u8a00\u8a9e\u3067\u3082\u5b58\u5728\u3059\u308b\u6982\u5ff5\u30fb\u5b9f\u88c5\u3067\u3042\u308a\u3001\u8907\u6570\u306e\u5b9a\u6570\u3092\u4e00\u3064\u306b\u307e\u3068\u3081\u3066\u5b9a\u7fa9\u3057\u305f\u308a\u7ba1\u7406\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-typescript\">enum SIZE {\n  Small,\n  Medium,\n  Large,\n}\n\nconst newSize = SIZE.Small;\n\nconsole.log(newSize === SIZE.Small);  \/\/ true\nconsole.log(newSize === SIZE.Large);  \/\/ false<\/code><\/pre>\n<h3>Enum (\u5217\u6319\u578b) \u3092\u4f7f\u3046\u30e1\u30ea\u30c3\u30c8<\/h3>\n<p>\u4e0a\u8a18\u306e\u4f8b\u3001\u5358\u7d14\u306b\u6587\u5b57\u5217\u3068\u3057\u3066 &quot;small&quot; \u306a\u3069\u3092\u5909\u6570\u3067\u5b9a\u7fa9\u3057\u3066\u6bd4\u8f03\u3059\u308b\u65b9\u6cd5\u3068\u6bd4\u3079\u3066\u3069\u3093\u306a\u30e1\u30ea\u30c3\u30c8\u304c\u3042\u308b\u306e\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n<p>\u6700\u3082\u5927\u304d\u306a\u6069\u6075\u306f\u3001\u30b9\u30da\u30eb\u30df\u30b9\u3084\u5927\u6587\u5b57\u5c0f\u6587\u5b57\u306a\u3069\u306e\u4eba\u7684\u30a8\u30e9\u30fc\u3092\u9632\u3050\u3053\u3068\u304c\u3067\u304d\u308b\u70b9\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070 Visual Studio Code \u306a\u3069\u306e TypeScript \u306e\u88dc\u5b8c\u304c\u53ef\u80fd\u306a\u30a8\u30c7\u30a3\u30bf\u4e0a\u3067 <code>SIZE.<\/code> \u3068\u5165\u529b\u3059\u308b\u3068\u3001 enum <code>SIZE<\/code> \u3067\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u30ad\u30fc\u304c\u30b5\u30b8\u30a7\u30b9\u30c8\u3055\u308c\u307e\u3059\u3002<br \/>\n<img src=\"https:\/\/storage.googleapis.com\/zenn-user-upload\/30c0e217b069-20211129.png\" alt=\"\" \/><\/p>\n<p>\u307e\u305f\u3001Enum\u306f\u578b\u3068\u3057\u3066\u3082\u5229\u7528\u3067\u304d\u308b\u306e\u3067\u3001\u3088\u308a\u5805\u7262\u306a\u30b7\u30b9\u30c6\u30e0\u306e\u69cb\u7bc9\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002<\/p>\n<h2>JavaScript\u3001TypeScript \u3067\u306e enum<\/h2>\n<p>JavaScript \u306b\u306f enum \u306f\u5b58\u5728\u3057\u307e\u305b\u3093\u3002\u305d\u306e\u305f\u3081\u3001enum \u3067\u3084\u308a\u305f\u3044\u3053\u3068\u3092\u5b9f\u73fe\u3059\u308b\u306b\u306f const \u304c\u4ee3\u7528\u3068\u306a\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<pre><code class=\"language-javascript\">  const SIZE = {\n    Small: &#039;small&#039;,\n    Medium: &#039;medium&#039;,\n    Large: &#039;large&#039;,\n  };\n  console.log(SIZE.Small); \/\/ small<\/code><\/pre>\n<p>\u4e00\u65b9 TypeScript \u306b\u306f enum \u306e\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5c11\u3005\u7656\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-typescript\">\/\/ \u4f55\u3082\u6307\u5b9a\u3057\u306a\u3044\u5834\u5408\u30010\u304b\u3089\u306e\u756a\u53f7\u304c\u5272\u308a\u632f\u3089\u308c\u3066\u3044\u304d\u307e\u3059\u3002\nenum SIZE {\n  Small, \/\/ 0\n  Medium, \/\/ 1\n  Large,  \/\/ 2\n}\nconsole.log(SIZE.Large);  \/\/ 2\nconsole.log(SIZE[1]); \/\/ Medium<\/code><\/pre>\n<p>\u5c11\u3057\u5197\u9577\u3067\u3059\u304c\u3001\u5404\u5b9a\u6570\u306b<strong>\u4efb\u610f\u306e\u6587\u5b57\u5217<\/strong>\u3084<strong>\u4efb\u610f\u306e\u6570\u5b57<\/strong>\u3092\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-typescript\">enum SIZE {\n  Small = &quot;small&quot;,\n  Medium = &quot;medium&quot;,\n  Large = &quot;large&quot;,\n}\nconsole.log(SIZE.Large);  \/\/ large<\/code><\/pre>\n<pre><code class=\"language-typescript\">\/\/ \u4e00\u756a\u6700\u521d\u306e\u5b9a\u6570(\u3053\u3053\u3067\u306fSmall)\u306b5\u3092\u6307\u5b9a\u3057\u305f\u5834\u5408\u3001\u5b9a\u6570\u306f5\u304b\u3089\u306e\u6570\u5b57\u304c\u5272\u308a\u632f\u3089\u308c\u307e\u3059\u3002\nenum SIZE {\n  Small = 5, \/\/ 5\n  Medium, \/\/ 6\n  Large,  \/\/ 7\n}\nconsole.log(SIZE.Large);  \/\/ 7<\/code><\/pre>\n<p>\u5b9a\u6570\u306e\u9014\u4e2d\u3067\u6570\u5b57\u3092\u6307\u5b9a\u3057\u305f\u5834\u5408\u3001\u6700\u521d\u306e\u5b9a\u6570\u306f0\u304b\u3089\u59cb\u307e\u308a\u3001\u9014\u4e2d\u304b\u3089\u6307\u5b9a\u3055\u308c\u305f\u6587\u5b57\u304b\u3089\u6570\u5b57\u304c\u5272\u308a\u632f\u3089\u308c\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-typescript\">enum SIZE {\n  Small, \/\/ 0\n  Medium = 4, \/\/ 4\n  Large,  \/\/ 5\n}\nconsole.log(SIZE.Medium) \/\/ 4<\/code><\/pre>\n<h2>Enum \u578b \u3068 Union\u578b\u306e\u6bd4\u8f03\u306b\u3064\u3044\u3066<\/h2>\n<p>\u3053\u308c\u307e\u3067\u8ff0\u3079\u305f\u30e1\u30ea\u30c3\u30c8\u3092\u8e0f\u307e\u3048\u305f\u4e0a\u3067\u3001\u5b9f\u306f\u754c\u9688\u3067\u306f TypeScript \u306e enum \u306b\u3064\u3044\u3066\u306f\u5229\u7528\u306e\u53cd\u5bfe\u6d3e(\u975e\u63a8\u5968\u6d3e\uff1f)\u304c\u5b58\u5728\u3057\u307e\u3059\u3002<br \/>\n\u3053\u306eenum\u578b\u3068\u3001\u975e\u63a8\u5968\u6d3e\u304c\u63a8\u5968\u3059\u308b<strong>union\u578b<\/strong>\u306b\u3064\u3044\u3066\u304a\u8a71\u3057\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h3>Enum \u3092\u4f7f\u7528\u3059\u308b\u969b\u306e\u6ce8\u610f\u70b9<\/h3>\n<h4>1. \u610f\u56f3\u3057\u306a\u3044\u5024\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u3066\u3057\u307e\u3046<\/h4>\n<p>\u4e0a\u8a18\u306e\u8aac\u660e\u3067\u3001enum\u306b\u6570\u5024\u304c\u5272\u308a\u5f53\u3066\u3089\u308c\u3066\u3044\u308b\u5834\u5408\u3001\u305d\u306e\u6570\u5024\u304b\u3089\u5024\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3068\u8aac\u660e\u3057\u307e\u3057\u305f\u3002\u3053\u308c\u3092\u5229\u7528\u3057\u3066\u3001\u5272\u308a\u5f53\u3066\u3089\u308c\u3066\u3044\u306a\u3044\u6570\u5024\u304b\u3089\u306e\u5024\u306e\u53d6\u5f97\u304c\u53ef\u80fd\u3068\u306a\u308a\u3001\u610f\u56f3\u305b\u305a<code>undefined<\/code>\u304c\u8fd4\u3063\u3066\u304d\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-typescript\">enum SIZE {\n  Small,\n  Medium,\n  Large,\n}\n\n  console.log(SIZE[1]);    \/\/ Medium\n  \/\/ no error!!\n  console.log(SIZE[5]);    \/\/ undefined<\/code><\/pre>\n<p>\u6570\u5024\u306eenum\u306b\u306f\u3042\u3089\u3086\u308bnumber\u3092\u5272\u308a\u5f53\u3066\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001\u578b\u5b89\u5168\u6027\u306b\u6b20\u3051\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<h4>2. Tree-shaking \u304c\u4f5c\u52d5\u305b\u305a\u3001\u547c\u3073\u51fa\u3055\u308c\u306a\u3044\u4e0d\u8981\u306a\u30b3\u30fc\u30c9\u304c\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u3066\u3057\u307e\u3046<\/h4>\n<p>TypeScript\u3067\u5b9a\u7fa9\u3055\u308c\u305fenum\u306f\u3001Javascript\u306b\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30eb\u3055\u308c\u308b\u969b\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a<strong>\u5373\u6642\u5b9f\u884c\u95a2\u6570<\/strong>\u3068\u3057\u3066\u5909\u63db\u3055\u308c\u307e\u3059\u3002\u5373\u6642\u5b9f\u884c\u95a2\u6570\u3068\u306f\u3001\u300c\u5b9a\u7fa9\u3055\u308c\u308b\u3068\u3059\u3050\u306b\u5b9f\u884c\u3055\u308c\u308b\u300dJavaScript\u306e\u95a2\u6570\u306e\u3053\u3068\u3067\u3059\u3002<\/p>\n<pre><code class=\"language-javascript\">var SIZE;\n(function (SIZE) {\n    SIZE[SIZE[&quot;Small&quot;] = 0] = &quot;Small&quot;;\n    SIZE[SIZE[&quot;Medium&quot;] = 4] = &quot;Medium&quot;;\n    SIZE[SIZE[&quot;Large&quot;] = 5] = &quot;Large&quot;;\n})(SIZE || (SIZE = {}));\n\n\/\/ \u6587\u5b57\u5217\u3092\u5272\u308a\u5f53\u3066\u305f\u5834\u5408\nvar SIZE;\n(function (SIZE) {\n    SIZE2[&quot;Small&quot;] = &quot;small&quot;;\n    SIZE2[&quot;Medium&quot;] = &quot;medium&quot;;\n    SIZE2[&quot;Large&quot;] = &quot;large&quot;;\n})(SIZE || (SIZE = {}));<\/code><\/pre>\n<p>\u3053\u306e\u3088\u3046\u306b\u5373\u6642\u5b9f\u884c\u95a2\u6570\u3068\u3057\u3066\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30eb\u3055\u308c\u308b\u3068\u3001Tree-shaking\u304c\u4e0a\u624b\u304f\u50cd\u304b\u306a\u304f\u306a\u3063\u3066\u3057\u307e\u3046\u3068\u3044\u3046\u554f\u984c\u70b9\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>Tree-shaking\u3068\u306f....?<br \/>\n<strong>Tree-shaking<\/strong> \u3068\u306f\u3001webpack\u306a\u3069\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u30cf\u30f3\u30c9\u30e9\u304cJavascript\u30d5\u30a1\u30a4\u30eb\u3092\u4e00\u3064\u306b\u307e\u3068\u3081\u308b\u969b\u306b\u3001\u5b9f\u884c\u3055\u308c\u306a\u3044\u4f59\u5206\u306a\u30b3\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u3068\u3044\u3063\u305f\u51e6\u7406\u306e\u3053\u3068\u3067\u3059\u3002<br \/>\n\u6728\u3092\u632f\u308b\u3053\u3068\u3067\u3001\u4e0d\u8981\u306a\u3082\u306e\u3092\u843d\u3068\u3057\u3066\u30dc\u30ea\u30e5\u30fc\u30e0\u3092\u5c0f\u3055\u304f\u3059\u308b\u30a4\u30e1\u30fc\u30b8\u3060\u3068\u500b\u4eba\u7684\u306b\u89e3\u91c8\u3057\u307e\u3057\u305f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u306e\u30b5\u30a4\u30c8\u3067 <code>Tree shaking<\/code>\u306b\u3064\u3044\u3066\u8aac\u660e\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u3088\u3051\u308c\u3070\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Glossary\/Tree_shaking\">MDN: Web Docs: Tree shaking<\/a><\/li>\n<li><a href=\"https:\/\/linguinecode.com\/post\/reduce-css-file-size-webpack-tree-shaking\">Webpack rule that may reduce your CSS file size dramatically<\/a><\/li>\n<\/ul>\n<h4>3. <code>isolatedModules<\/code>\u304ctrue\u306e\u5834\u5408\u3001const enum\u4f7f\u7528\u6642\u306b\u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3053\u3068\u304c\u3042\u308b<\/h4>\n<p><code>--isolatedModules<\/code>\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6709\u52b9\u306b\u3059\u308b\u5834\u5408\u3001<\/p>\n<blockquote>\n<p>Transpile each file as a separate module (similar to 'ts.transpileModule').<br \/>\n\u3053\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3092true\u306b\u3057\u305f\u5834\u5408\u3001const enum\u306f\u30a8\u30e9\u30fc\u3068\u306a\u308a\u30b3\u30f3\u30d1\u30a4\u30eb\u3067\u304d\u306a\u3044\u3002<\/p>\n<\/blockquote>\n<p>\u8a73\u7d30\u306f\u4ee5\u4e0b\u306e\u8a18\u4e8b\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<br \/>\n<a href=\"https:\/\/www.kabuku.co.jp\/developers\/good-bye-typescript-enum\">https:\/\/www.kabuku.co.jp\/developers\/good-bye-typescript-enum<\/a><\/p>\n<p>\u4ee5\u4e0a\u306e\u7406\u7531\u304b\u3089\u3001enum\u306e\u5229\u7528\u306f\u907f\u3051\u308b\u3079\u304d\u3067\u3042\u308b\u3068\u8a00\u308f\u308c\u3066\u3044\u307e\u3059\u3002<br \/>\n\u3067\u306f\u3001enum\u306e\u3088\u3046\u306a\u6a5f\u80fd\u3092\u4f7f\u3044\u305f\u3044\u5834\u5408\u306f\u3069\u3046\u3059\u308c\u3070\u826f\u3044\u306e\u3067\u3057\u3087\u3046\u304b?<br \/>\n\u305d\u308c\u306f\u3001Union\u578b\u3092\u4f7f\u3046\u3053\u3068\u3067\u5b9f\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>Union\u578b\u306b\u3064\u3044\u3066<\/h3>\n<p>Union\u578b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b2\u3064\u4ee5\u4e0a\u306e\u578b\u3092\u30d1\u30a4\u30d7\u8a18\u53f7<code>|<\/code>\u3067\u7e4b\u3052\u3066\u66f8\u304f\u3053\u3068\u3067\u3001\u8907\u6570\u306e\u578b\u3092\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-typescript\">const middleName: string | null<\/code><\/pre>\n<h2>Enum\u306e\u4ee3\u308f\u308a\u306b\u3001Union\u578b\u3092\u7528\u3044\u308b<\/h2>\n<p>Enum\u3092\u4f7f\u308f\u305a\u3068\u3082\u3001Enum\u306e\u3088\u3046\u306b\u5b9a\u7fa9\u3059\u308b\u306b\u306f\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u306e\u3067\u3057\u3087\u3046\u304b?<br \/>\n<code>const assertion(as const)<\/code>, <code>keyof<\/code>, <code>typeof<\/code> \u3092\u4f7f\u3063\u3066enum\u306e\u3088\u3046\u306a\u5b9a\u7fa9\u3092\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-typescript\">\/\/ const assertion\u3092\u4f7f\u3063\u3066\u3001\u30ea\u30c6\u30e9\u30eb\u578b\u3068\u3057\u3066\u6271\u3046\nconst SizeType = {\n  SMALL: &#039;small&#039;,\n  MEDIUM: &#039;medium&#039;,\n  LARGE: &#039;large&#039;,\n} as const;\n\n\/\/ type SizeType = &#039;small&#039; | &#039;medium&#039; | &#039;large&#039;\ntype SizeType = typeof SizeType[keyof typeof SizeType];<\/code><\/pre>\n<p>\u7d30\u304b\u304f\u5206\u89e3\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-typescript\">  \/\/   type SizeType = {\n  \/\/     readonly SMALL: &quot;small&quot;;\n  \/\/     readonly MEDIUM: &quot;medium&quot;;\n  \/\/     readonly LARGE: &quot;large&quot;;\n  \/\/ }\n  type SizeType = typeof Size;\n\n  \/\/ type SizeKey = &quot;SMALL&quot; | &quot;MEDIUM&quot; | &quot;LARGE&quot;\n  type SizeKey = keyof SizeType;\n\n  \/\/ type Size = &#039;small&#039; | &#039;medium&#039; | &#039;large&#039;\n  type Size = SizeType[SizeKey];<\/code><\/pre>\n<p>const assertion\u3001\u30ea\u30c6\u30e9\u30eb\u578b, keyof, typeof \u306b\u95a2\u3057\u3066\u306f\u3001\u4ee5\u4e0b\u306e\u8a18\u4e8b\u3067\u308f\u304b\u308a\u3084\u3059\u304f\u8aac\u660e\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001\u3088\u3051\u308c\u3070\u3054\u78ba\u8a8d\u304f\u3060\u3055\u3044\u3002<\/p>\n<ul>\n<li><a href=\"https:\/\/book.yyts.org\/reference\/values-types-variables\/const-assertion\">const\u30a2\u30b5\u30fc\u30b7\u30e7\u30f3\u300cas const\u300d (const assertion)<\/a><\/li>\n<li><a href=\"https:\/\/book.yyts.org\/reference\/values-types-variables\/literal-types\">\u30ea\u30c6\u30e9\u30eb\u578b<\/a><\/li>\n<li><a href=\"https:\/\/www.wakuwakubank.com\/posts\/756-typescript-in-typeof-keyof\/\">typeof, keyof, in\u306e\u52d5\u4f5c\u78ba\u8a8d<\/a><\/li>\n<\/ul>\n<h3>union\u306e\u30ea\u30b9\u30c8\u3092\u624b\u306b\u5165\u308c\u305f\u3044\u5834\u5408<\/h3>\n<p>javaScript\u304c\u63d0\u4f9b\u3059\u308b Object.values\u95a2\u6570\u3092\u4f7f\u7528\u3057\u3066\u3001\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-typescript\">\/\/ const AllSizeType = Object.values(SizeType);\nconst AllSizeType = Object.values(SizeType);<\/code><\/pre>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Object\/values\">MDN Web Docs: Object.values<\/a><\/li>\n<\/ul>\n<h2>\u5b8c\u6210\u7cfb\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8<\/h2>\n<p>WESEEK \u3067\u306f\u4ee5\u4e0b\u3092\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3068\u3057\u3066\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\u3002const assertion \u306b\u5024\u3092\u8ffd\u52a0\u3059\u308b\u3060\u3051\u3067 union \u3068\u5168\u5024\u306e\u30ea\u30b9\u30c8\u3092 export \u3067\u304d\u308b\u306e\u3067\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<pre><code class=\"language-typescript\">const SizeType = {\n  SMALL: &#039;small&#039;,\n  MEDIUM: &#039;medium&#039;,\n  LARGE: &#039;large&#039;,\n} as const;\n\n\/\/ type SizeType = &quot;small&quot; | &quot;medium&quot; | &quot;large&quot;\nexport type SizeType = typeof SizeType[keyof typeof SizeType];\n\/\/ \u5168\u3066\u306etype\u3092\u914d\u5217\u3068\u3057\u3066\u53d6\u5f97\n\/\/ const AllSizeType: (&quot;small&quot; | &quot;medium&quot; | &quot;large&quot;)[]\nexport const AllSizeType = Object.values(SizeType);<\/code><\/pre>\n<h2>\u307e\u3068\u3081<\/h2>\n<ul>\n<li>\u7dcf\u79f0\u3068\u3057\u3066\u306e Enum (\u5217\u6319\u578b)\u306b\u3064\u3044\u3066\n<ul>\n<li>\u8907\u6570\u306e\u5b9a\u6570\u3092\u307e\u3068\u3081\u3066\u5b9a\u7fa9\u3057\u305f\u308a\u7ba1\u7406\u3059\u308b\u306e\u306b\u4fbf\u5229<\/li>\n<li>\u3044\u308d\u3093\u306a\u8a00\u8a9e\u3067\u5229\u7528\u3067\u304d\u308b\u304c JavaScript \u306b\u306f\u306a\u3044<\/li>\n<li>Enum \u306f\u578b\u3068\u3057\u3066\u3082\u4f7f\u3048\u308b\u306e\u3067\u3001\u30d0\u30b0\u3092\u9632\u3050\u3053\u3068\u306b\u5f79\u7acb\u3064<\/li>\n<\/ul>\n<\/li>\n<li>TypeScript \u306e enum vs union\n<ul>\n<li>\u3044\u308d\u3093\u306a\u8a18\u8ff0\u65b9\u6cd5\u304c\u3042\u308b<\/li>\n<li>enum \u306f\u3001\u8a18\u8ff0\u306f\u7c21\u5358<\/li>\n<li>enum \u306e\u5229\u7528\u306b\u306f\u8907\u6570\u306e\u843d\u3068\u3057\u7a74\u304c\u3042\u308b\u306e\u3067\u3001union\u578b\u3092\u7528\u3044\u308b\u3053\u3068\u304c\u4e00\u90e8\u3067\u306f\u63a8\u5968\u3055\u308c\u3066\u3044\u308b<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u3053\u3060\u308f\u308a\u304c\u306a\u3051\u308c\u3070\u3001TypeScript \u3067\u306fenum\u578b\u3067\u306f\u306a\u304funion\u578b\u3092\u7528\u3044\u308b\u3053\u3068\u3092\u304a\u5968\u3081\u3057\u307e\u3059\u3002<\/p>\n<h2>\u53c2\u8003\u306b\u3055\u305b\u3066\u3044\u305f\u3060\u3044\u305f\u30b5\u30a4\u30c8<\/h2>\n<ul>\n<li><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Glossary\/Tree_shaking\">MDN Web Docs: Tree shaking<\/a><\/li>\n<li><a href=\"https:\/\/www.cyokodog.net\/blog\/typescript-enum-replacement\/\">Enum \u306e\u4ee3\u308f\u308a\u306b\u4f7f\u3046 union \u578b<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u6295\u7a3f\u306f\u3001\u5f0a\u793e\u304c\u63d0\u4f9b\u3059\u308bWESEEK TECH\u901a\u4fe1\u306e\u4e00\u74b0\u3067\u3059\u3002 WESEEK TECH\u901a\u4fe1\u3068\u306f\u3001WESEEK\u306e\u30a8\u30f3\u30b8\u30cb\u30a2\u304c\u30ad\u30e3\u30c3\u30c1\u30a2\u30c3\u30d7\u3057\u305f\u6280\u8853\u306b\u95a2\u3059\u308b\u60c5\u5831\u3092\u3001tech\u30d6\u30ed\u30b0\u3092\u901a\u3058\u3066\u5b9a\u671f\u7684\u306b\u767a\u4fe1\u3057\u3066\u3044\u304f\u3082\u306e\u3067\u3059\u3002 \u306f\u3058<\/p>\n","protected":false},"author":3,"featured_media":2113,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[186,57,187],"tags":[],"_links":{"self":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/1609"}],"collection":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/comments?post=1609"}],"version-history":[{"count":20,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/1609\/revisions"}],"predecessor-version":[{"id":4031,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/1609\/revisions\/4031"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media\/2113"}],"wp:attachment":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media?parent=1609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/categories?post=1609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/tags?post=1609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}