{"id":10092,"date":"2020-04-18T18:16:38","date_gmt":"2020-04-18T09:16:38","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=10092"},"modified":"2023-05-27T23:28:38","modified_gmt":"2023-05-27T14:28:38","slug":"%e3%80%90typescript%e3%80%91%e3%80%8ctupple%e5%9e%8b%e3%80%8d%e3%81%ab%e3%81%a4%e3%81%84%e3%81%a6","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=10092","title":{"rendered":"\u3010TypeScript\u3011\u300c\u5171\u7528\u4f53\u578b\uff08union\u578b\uff09\u300d\u300cTuple(\u30bf\u30d7\u30eb)\u578b\u300d\u3001\u300cLiteral\u578b\u300d\u3001\u300c\u5217\u6319\u578b(enum)\u300d\u3001\u300ctype guard\u300d"},"content":{"rendered":"<h2>\u5171\u7528\u4f53\u578b\uff08union\u578b\uff09\u3068\u306f\uff1f<\/h2>\n<h3>\u8a18\u8ff0\u65b9\u6cd5<\/h3>\n<p>\u4e0b\u8a18\u306e\u4f8b\u306e\u5834\u5408\u306fstring\u3067\u3082number\u3067\u3082\u3069\u3061\u3089\u306e\u578b\u3067\u3082\u4ee3\u5165\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3088\u3068\u3044\u3046\u5834\u5408\u306e\u578b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">let a: string | number;\r\na = '\u3042';\r\na = 2;<\/pre>\n<h2>Tuple(\u30bf\u30d7\u30eb)\u578b\u3068\u306f\uff1f<\/h2>\n<p>\u914d\u5217\u306e\u8981\u7d20\u306e\u578b\u304c\u660e\u78ba\u306b\u308f\u304b\u3063\u3066\u3044\u308b\u5834\u5408\u306b\u4f7f\u3044\u307e\u3059\u3002<span style=\"color: #ff0000;\"><strong>\u5171\u7528\u578b\u3067\u3082\u540c\u69d8\u306e\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u304c\u5177\u4f53\u7684\u306b\u8981\u7d20\u306e\u9806\u756a\u3082\u6307\u5b9a\u3057\u3066\u5236\u7d04\u3092\u5f37\u304f\u3057\u305f\u3044\u3068\u3044\u3046\u5834\u5408<\/strong><\/span>\u306bTuple\u578b\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<h3>\u8a18\u8ff0\u65b9\u6cd5<\/h3>\n<pre class=\"lang:default decode:true \">let tupple: [number, string] = [123, 'str'];<\/pre>\n<h3>\u6ce8\u610f\u70b9<\/h3>\n<p>\u5f8c\u304b\u3089push\u306a\u3069\u3067\u8ffd\u52a0\u3059\u308b\u5834\u5408\u306f\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u305b\u3093\u3002TypeScript\u306e\u601d\u60f3\u7684\u306b\u306f\u521d\u671f\u5024\u306e\u30c1\u30a7\u30c3\u30af\u306f\u3057\u3063\u304b\u308a\u3084\u3063\u3066\u304f\u308c\u308b\u306e\u3067\u3059\u304c\u5f8c\u304b\u3089\u8ffd\u52a0\u3059\u308b\u5834\u5408\u306a\u3069\u306f\u9762\u5012\u307f\u3066\u304f\u308c\u306a\u3044\u306e\u3067\u305d\u3053\u306f\u6ce8\u610f\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>Literal\u578b\u3068\u306f\uff1f<\/h2>\n<p>\u4f7f\u3063\u3066\u6b32\u3057\u3044\u30c7\u30fc\u30bf\u578b\u3060\u3051\u3067\u306a\u304f\u3001<span style=\"color: #ff0000;\"><strong>\u4f7f\u3063\u3066\u6b32\u3057\u3044\u5177\u4f53\u7684\u306a\u5024\u307e\u3067\u7e1b\u308b\u30c7\u30fc\u30bf\u578b\u306e\u3053\u3068<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<h3>\u66f8\u304d\u65b9<\/h3>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b\u7e26\u68d2\u3067\u7e4b\u304e\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">let kanjou: '\u559c' | '\u6012' | '\u54c0' | '\u697d' = '\u697d';\r\nkanjou = '\u6012';   \/\/OK\r\nkanjou = '\u51b7\u9759'; \/\/\u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3002<\/pre>\n<h3>const\u306e\u578b\u63a8\u8ad6<\/h3>\n<p>\u306a\u304a\u3001const\u3092\u4f7f\u3046\u3053\u3068\u306b\u3088\u3063\u3066\u81ea\u52d5\u7684\u306b\u578b\u63a8\u8ad6\u3067\u30ea\u30c6\u30e9\u30eb\u578b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">const hello = \"\u3053\u3093\u306b\u3061\u306f\";<\/pre>\n<p>\u4f8b\u3048\u3070\u4e0a\u8a18\u306e\u3088\u3046\u306a\u8a18\u8ff0\u306a\u3089\u300c\"\u3053\u3093\u306b\u3061\u306f\"\u300d\u3068\u3044\u3046\u6587\u5b57\u3092\u6271\u3046\u30ea\u30c6\u30e9\u30eb\u578b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u5217\u6319\u578b(enum)\u3068\u306f\uff1f<\/h2>\n<p>enum\u306b\u306f\u4e8c\u3064\u306e\u4f7f\u3044\u9053\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3>\u6587\u5b57\u5217\u306e\u5217\u6319<\/h3>\n<pre class=\"lang:default decode:true\">enum fruits {\r\n  apple = \"\u6797\u6a8e\",\r\n  orange = \"\u30aa\u30ec\u30f3\u30b8\",\r\n  meron = \"\u30e1\u30ed\u30f3\",\r\n}<\/pre>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u5217\u6319\u578b\u4ee5\u5916\u306e\u5024\u3092\u518d\u4ee3\u5165\u3057\u3088\u3046\u3068\u3057\u3066\u3082\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u3059\u3002\u901a\u5e38\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306a\u3089\u3053\u3046\u306f\u306a\u3089\u306a\u3044\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">enum fruits {\r\n  apple = \"\u6797\u6a8e\",\r\n  orange = \"\u30aa\u30ec\u30f3\u30b8\",\r\n  meron = \"\u30e1\u30ed\u30f3\",\r\n}\r\n\r\nlet fruit = fruits.apple;\r\nfruit = \"\u30ec\u30e2\u30f3\"; \/\/ NG\r\nfruit = fruits.meron; \/\/ OK<\/pre>\n<h4>Literal\u578b\u306eUnion\u3068\u306e\u6bd4\u8f03<\/h4>\n<p>\u3067\u306f\u3001Literal\u306eUnion('apple' | 'orange'\u307f\u305f\u3044\u306a\u578b\uff09\u3068\u6bd4\u3079\u308b\u3068\u3001enum\u306f\u660e\u793a\u7684\u306b\u4f7f\u3046\u6587\u5b57\u5217\u3092\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>\u6570\u5024\u306e\u5217\u6319<\/h3>\n<p>\u5217\u6319\u578b\u3092\u4f7f\u7528\u3059\u308b\u4e8b\u3067<span style=\"color: #ff0000;\"><strong>0\u756a\u304b\u3089\u9806\u756a\u306b\u756a\u53f7\u3092\u632f\u3063\u3066\u3044\u304f\u4e8b\u304c\u53ef\u80fd<\/strong><\/span>\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">enum Week {\r\n  San,\r\n  Mon,\r\n  Tue,\r\n  Wen,\r\n  Thu,\r\n  Fri,\r\n  Sat,\r\n}\r\n\r\n\/\/0\u304c\u51fa\u529b\u3055\u308c\u308b\u3002\r\nconsole.log(Week.San);<\/pre>\n<h4>\u9023\u756a\u306e\u521d\u671f\u5024\u3092\u5909\u3048\u308b\u306b\u306f\uff1f<\/h4>\n<p>\u901a\u5e38\u306f\u9023\u756a\u306f0\u304b\u3089\u59cb\u307e\u308a\u307e\u3059\u304c\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067<span style=\"color: #ff0000;\"><strong>\u5225\u756a\u53f7\u304b\u3089\u59cb\u3081\u308b\u4e8b\u304c\u53ef\u80fd<\/strong><\/span>\u3067\u3059\u3002\u4e0b\u8a18\u306e\u4f8b\u3067\u8a00\u3048\u30701\u304b\u3089\u9806\u756a\u306b\u9023\u756a\u304c\u3075\u3089\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">enum Week {\r\n  San = 1,\r\n  Mon,\r\n  Tue,\r\n  Wen,\r\n  Thu,\r\n  Fri,\r\n  Sat,\r\n}<\/pre>\n<h4>\u30e1\u30ea\u30c3\u30c8<\/h4>\n<p>\u3053\u308c\u3092\u666e\u901a\u306eJavaScript\u30b3\u30fc\u30c9\u3067\u5b9f\u73fe\u3057\u3088\u3046\u3068\u3059\u308b\u3068<span style=\"color: #ff0000;\"><strong>\u9023\u60f3\u914d\u5217\u3092\u4f5c\u3063\u3066\u756a\u53f7\u30921\u3064\uff11\u3064\u632f\u3063\u3066\u3044\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u305d\u3046\u3057\u305f\u5834\u5408\u306f\u4eba\u624b\u3092\u4ecb\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u306e\u3067\u30df\u30b9\u304c\u767a\u751f\u3057\u3084\u3059\u3044<\/strong><\/span>\u3067\u3059\u3057\u3001\u6b20\u756a\u53f7\u304c\u767a\u751f\u3057\u306a\u3044\u3067\u3059\u3002<\/p>\n<h2 class=\"title short_title\">type guard<\/h2>\n<p>TypeScript\u3067\u5909\u6570\u306e\u578b\u304cUnion\u3068\u304b\u3067\u8907\u6570\u5b58\u5728\u3057\u3046\u308b\u5834\u5408\u306b\u578b\u3092\u7279\u5b9a\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<h3>typeof\u3092\u4f7f\u3046\u3002<\/h3>\n<p>\u8a72\u5f53\u306e\u578b\u3067\u3042\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002TypeScript\u306e\u6587\u6cd5\u3068\u3044\u3046\u3088\u308a\u306fJavaScript\u306e\u6587\u6cd5\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">if (typeof a === \"string\") {\r\n   a.toUpperCase;\r\n}<\/pre>\n<h3>in\u3092\u4f7f\u3046<\/h3>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u8a72\u5f53\u306e\u30ad\u30fc\u304c\u3042\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002TypeScript\u306e\u6587\u6cd5\u3068\u3044\u3046\u3088\u308a\u306fJavaScript\u306e\u6587\u6cd5\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">if ('name' in aObj) {\r\n  aObj.name\r\n}<\/pre>\n<h3>instanceof\u3092\u4f7f\u3046<\/h3>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306bCar\u30af\u30e9\u30b9\u3068AirPlane\u30af\u30e9\u30b9\u306e\u3069\u3061\u3089\u304b\u3060\u3063\u305f\u5834\u5408\u3067\u3082Car\u30af\u30e9\u30b9\u3067\u3042\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002\u3053\u308c\u3082JS\u306b\u5b58\u5728\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">a: Car | AirPlane;\r\n\r\nif (a instanceof Car) {\r\n  a.run;\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"\u5171\u7528\u4f53\u578b\uff08union\u578b\uff09\u3068\u306f\uff1f \u8a18\u8ff0\u65b9\u6cd5 \u4e0b\u8a18\u306e\u4f8b\u306e\u5834\u5408\u306fstring\u3067\u3082number\u3067\u3082\u3069\u3061\u3089\u306e\u578b\u3067\u3082\u4ee3\u5165\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3088\u3068\u3044\u3046\u5834\u5408\u306e\u578b\u306b\u306a\u308a\u307e\u3059\u3002 let a: string | number; a = '\u3042'; [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[101],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/10092"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10092"}],"version-history":[{"count":10,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/10092\/revisions"}],"predecessor-version":[{"id":20608,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/10092\/revisions\/20608"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10092"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}