{"id":13492,"date":"2021-05-22T21:13:49","date_gmt":"2021-05-22T12:13:49","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=13492"},"modified":"2023-05-27T23:25:05","modified_gmt":"2023-05-27T14:25:05","slug":"%e3%80%90typescript%e3%80%91%e3%80%8cas%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=13492","title":{"rendered":"\u3010TypeScript\u3011\u300cas\u300d\u3001\u300cany\u300d\u3001\u300c?\u300d\u3001\u300c!\u300d\u3001\u300c\u578b\u306e\u4e92\u63db\u6027\u300d\u3001\u300c\u30a2\u30b5\u30fc\u30b7\u30e7\u30f3\u300d\u306b\u3064\u3044\u3066"},"content":{"rendered":"<h2>as\u3001any<\/h2>\n<p>TypeScript\u304c\u63d0\u4f9b\u3059\u308b\u578b\u5b89\u5168\u3092\u5426\u5b9a\u3059\u308b\u884c\u70ba\u3067\u3042\u308a\u57fa\u672c\u7684\u306b\u4f7f\u3063\u3066\u306f\u3044\u3051\u306a\u3044\u3002<\/p>\n<h2>any\u3084as\u3092\u4f7f\u3063\u3066\u826f\u3044\u30b1\u30fc\u30b9<\/h2>\n<p>TypeScript\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u304c\u5b8c\u74a7\u3067\u306f\u306a\u304f\u4f7f\u7528\u3092\u4f59\u5100\u306a\u304f\u3055\u308c\u308b\u30b1\u30fc\u30b9\u304c\u3042\u308b\u3002<\/p>\n<h2>?(Optional Chaining)<\/h2>\n<p><span style=\"color: #ff0000;\"><strong>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff08\u914d\u5217\u3082\u542b\u3080\uff09\u306e\u5834\u5408\u3057\u304b\u4f7f\u7528\u3067\u304d\u306a\u3044\u3002<\/strong><\/span><\/p>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3084\u914d\u5217\u304c\u304cnull\u3084undefined\u306e\u5834\u5408\u306b\u30ad\u30fc\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3088\u3046\u3068\u3057\u305f\u969b\u306b\u3001undefined\u3092\u8fd4\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">console.log(obj?.prop); \/\/ \u30c9\u30c3\u30c8\u8868\u8a18\u6cd5\r\nconsole.log(obj?.['expr']); \/\/ \u30d6\u30e9\u30b1\u30c3\u30c8\u8868\u8a18\u6cd5\r\nconsole.log(array?.[0]); \/\/ \u914d\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u30a2\u30af\u30bb\u30b9\r\n<\/pre>\n<p>\u3053\u308c\u3092<span style=\"color: #ff0000;\"><strong>\u300cOptional Chaining\u300d<\/strong><\/span>\u3068\u547c\u3073\u307e\u3059\u3002<\/p>\n<h2>!<\/h2>\n<p>NULL\u3058\u3083\u306a\u3044\u3068\u8a00\u3044\u5207\u308b\u6587\u6cd5\u3002NonNullAssertionOperator\u3068\u3044\u3046TypeScript2.0\u304b\u3089\u767b\u5834\u3057\u305f\u6a5f\u80fd\u3067\u3069\u3093\u306a\u578b\u306b\u3082\u4f7f\u3048\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true\">const test = function(); \/\/ function\u306fstring | undefined\u3092\u8fd4\u3059\u95a2\u6570\u3002\r\ntest!\r\n<\/pre>\n<p>\u5909\u6570\u306e\u5f8c\u306b\u3064\u3051\u308b\u3068\u300cstring | undefined\u300d\u3068\u304b\u306e\u5834\u5408\u306fstring\u3068\u304b\u306b\u30ad\u30e3\u30b9\u30c8\u3057\u3066\u304f\u308c\u308b\u3002<\/p>\n<p>\u305f\u3060\u3001\u4f7f\u7528\u3059\u308b\u3068eslint\u3067\u4e0b\u8a18\u306e\u30a8\u30e9\u30fc\u304c\u51fa\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true\">warning  Forbidden non-null assertion  @typescript-eslint\/no-non-null-assertion<\/pre>\n<p>\u3082\u3057\u3001!\u3092\u4f7f\u7528\u3059\u308b\u5834\u5408\u306feslint\u306e\u89e3\u9664\u3092\u884c\u3046\u3002<\/p>\n<pre class=\"lang:default decode:true\">\/\/ eslint-disable-next-line @typescript-eslint\/no-non-null-assertion<\/pre>\n<h2>\u578b\u306e\u4e92\u63db\u6027<\/h2>\n<p>TypeScript\u306b\u304a\u3044\u3066<span style=\"color: #ff0000;\"><strong>\u3042\u308b\u578b\u3092\u4ed6\u306e\u578b\u306b\u4ee3\u5165\u3059\u308b\u4e8b\u304c\u53ef\u80fd\u304b\u3069\u3046\u304b<\/strong><\/span>\u3068\u3044\u3046\u6982\u5ff5\u306e\u3053\u3068\u3067\u3059\u3002\u4f8b\u3048\u3070\u4e0b\u8a18\u306e\u3088\u3046\u306bany\u578b\u306b\u306fstring\u578b\u3092\u4ee3\u5165\u3059\u308b\u4e8b\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">let a: any = '\u30c6\u30b9\u30c8';\r\nlet b: string = '\u30c6\u30b9\u30c82';\r\na = b;<\/pre>\n<h3>\u30af\u30e9\u30b9\u306e\u4ee3\u5165\u306b\u3064\u3044\u3066<\/h3>\n<p><span style=\"color: #ff0000;\"><strong>\u30e1\u30f3\u30d0\u30fc\u306e\u30c7\u30fc\u30bf\u578b\u306b\u4e92\u63db\u6027\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af<\/strong><\/span>\u3057\u3066\u3044\u307e\u3059\u3002\u3082\u3057\u4e92\u63db\u6027\u304c\u306a\u3044\u5834\u5408\u306f\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u3059\u3002\uff08\u306a\u304a\u3001\u30af\u30e9\u30b9\u306b\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u7b49\u306f\u5168\u304f\u8003\u616e\u306f\u3057\u307e\u305b\u3093\u3002<span style=\"color: #ff0000;\"><strong>\u3042\u304f\u307e\u3067\u4ee3\u5165\u3055\u308c\u308b\u5074\u3068\u4ee3\u5165\u3059\u308b\u5074\u306e\u5185\u90e8\u3067\u6301\u3063\u3066\u3044\u308b\u30e1\u30f3\u30d0\u306e\u30c7\u30fc\u30bf\u578b\u306e\u30c1\u30a7\u30c3\u30af\u306b\u306a\u308a\u307e\u3059\u3002<\/strong><\/span>\uff09<\/p>\n<h2>\u30a2\u30b5\u30fc\u30b7\u30e7\u30f3<\/h2>\n<h3>\u578b\u30a2\u30b5\u30fc\u30b7\u30e7\u30f3<\/h3>\n<p>\u4f8b\u3048\u3070\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u305f\u5834\u5408\u306bb\u306f\u6570\u5024\u304c\u4ee3\u5165\u3055\u308c\u3066\u3044\u307e\u3059\u304c\u3001\u666e\u901a\u306b\u305d\u306e\u5f8c\u306e\u6587\u5b57\u5217\u306e\u4ee3\u5165\u3082\u53ef\u80fd\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">let a: any = '\u30c6\u30b9\u30c8';\r\nlet b = a.length;\r\nb = '\u3042';<\/pre>\n<p>\u4e0b\u8a18\u306e\u3088\u3046\u306b<span style=\"color: #ff0000;\"><strong>\u300c\u5909\u6570 as \u30c7\u30fc\u30bf\u578b\u300d<\/strong><\/span>\u3068\u3059\u308b\u4e8b\u3067\u578b\u5909\u63db\u3057\u3066\u7570\u306a\u308b\u30c7\u30fc\u30bf\u578b\u4ee3\u5165\u6642\u306b\u30a8\u30e9\u30fc\u3068\u3059\u308b\u4e8b\u304c\u53ef\u80fd\u3067\u3059\u3002\u3053\u308c\u3092\u578b\u30a2\u30b5\u30fc\u30b7\u30e7\u30f3\u3068\u547c\u3073\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">let a: any = '\u30c6\u30b9\u30c8';\r\nlet b = (a as string).length;\r\nb = '\u3042'; \/\/\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3002<\/pre>\n<p>\u306a\u304a\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u304c\u3001\u3053\u308c\u306f<span style=\"color: #ff0000;\"><strong>React\u306e\u4e00\u90e8\u306e\u6587\u6cd5\u3068\u4f3c\u3066\u3044\u308b\u305f\u3081\u975e\u63a8\u5968<\/strong><\/span>\u3068\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true \">let a: any = '\u30c6\u30b9\u30c8';\r\nlet b = (&lt;string&gt;a).length;\r\nb = '\u3042'; \/\/\u30a8\u30e9\u30fc\u306b\u306a\u308b\u3002<\/pre>\n","protected":false},"excerpt":{"rendered":"as\u3001any TypeScript\u304c\u63d0\u4f9b\u3059\u308b\u578b\u5b89\u5168\u3092\u5426\u5b9a\u3059\u308b\u884c\u70ba\u3067\u3042\u308a\u57fa\u672c\u7684\u306b\u4f7f\u3063\u3066\u306f\u3044\u3051\u306a\u3044\u3002 any\u3084as\u3092\u4f7f\u3063\u3066\u826f\u3044\u30b1\u30fc\u30b9 TypeScript\u306e\u30b3\u30f3\u30d1\u30a4\u30eb\u304c\u5b8c\u74a7\u3067\u306f\u306a\u304f\u4f7f\u7528\u3092\u4f59\u5100\u306a\u304f\u3055\u308c\u308b\u30b1\u30fc\u30b9\u304c\u3042\u308b\u3002 ?(Op [&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\/13492"}],"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=13492"}],"version-history":[{"count":10,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13492\/revisions"}],"predecessor-version":[{"id":20600,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/13492\/revisions\/20600"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13492"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}