{"id":3958,"date":"2022-12-13T11:34:09","date_gmt":"2022-12-13T02:34:09","guid":{"rendered":"https:\/\/weseek.co.jp\/tech\/?p=3958"},"modified":"2023-04-17T10:25:37","modified_gmt":"2023-04-17T01:25:37","slug":"typescript%e3%81%aeis%e6%bc%94%e7%ae%97%e5%ad%90%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9f%e8%87%aa%e4%bd%9c%e5%9e%8b%e3%82%ac%e3%83%bc%e3%83%89%e3%82%92%e4%bd%bf%e3%81%86","status":"publish","type":"post","link":"https:\/\/weseek.co.jp\/tech\/3958\/","title":{"rendered":"TypeScript\u306eis\u6f14\u7b97\u5b50\u3092\u4f7f\u3063\u305f\u81ea\u4f5c\u578b\u30ac\u30fc\u30c9\u3092\u4f7f\u3046"},"content":{"rendered":"<h1>\u306f\u3058\u3081\u306b<\/h1>\n<p>\u3053\u3093\u306b\u3061\u306f WESEEK \u3067\u308f\u308a\u3068\u4f55\u3067\u3082\u3084\u3063\u3066\u3044\u308b haruhikonyan \u3067\u3059\u3002<br \/>\n\u307f\u306a\u3055\u3093 TypeScript \u66f8\u3044\u3066\u307e\u3059\u304b\uff1f<br \/>\n\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u306f\u3082\u3061\u308d\u3093 Node \u3067\u30b5\u30fc\u30d0\u30b5\u30a4\u30c9\u3092\u66f8\u3044\u3066\u3082\u3088\u3057\u3001\u3055\u3089\u306b\u578b\u5b89\u5168\uff01<br \/>\n\u305d\u3093\u306a\u578b\u5b89\u5168\u306a TypeScript \u3092\u3088\u308a\u5f37\u56fa\u306b\u4f7f\u3044\u3053\u306a\u3059\u305f\u3081\u306e User-Defined Type Guards \u306e\u4e00\u3064\u3067\u3042\u308b is \u6f14\u7b97\u5b50\u3092\u4f7f\u3063\u305f\u81ea\u4f5c\u578b\u30ac\u30fc\u30c9\u306e\u7d39\u4ecb\u3092\u3057\u307e\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<h1>\u578b\u30ac\u30fc\u30c9\u3068\u306f<\/h1>\n<p>\u307e\u305a\u6700\u521d\u306b\u578b\u30ac\u30fc\u30c9\u3068\u306f\u4f55\u304b\u3067\u3059\u3002<br \/>\n\u6982\u8981\u306f<a href=\"https:\/\/typescript-jp.gitbook.io\/deep-dive\/type-system\/typeguard\">\u53c2\u8003 URL<\/a> \u3092\u8aad\u3093\u3067\u3044\u305f\u3060\u3051\u308c\u3070\u3044\u3044\u3093\u3067\u3059\u304c\u3001\u304a\u305d\u3089\u304f\u4e00\u756a\u4f7f\u3046\u3060\u308d\u3046\u306a\u306e\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u3082\u306e\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-typescript\">type NullableString = string | null\nconst func = (nullableString: NullableString) =&gt; {\n  if (nullableString === null) {\n    \/\/ nullableString \u306f\u3053\u306e if \u6587\u306e\u4e2d\u3067\u306f null \u578b\u3068\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u3089\u306f\u89e3\u91c8\u3059\u308b\n    nullableString.length \/\/ &#039;nullableString&#039; is possibly &#039;null&#039;.(18047) \u3044\u308f\u3086\u308b\u306c\u308b\u307d\n    console.log(&quot;nullableString is null&quot;)\n  } else {\n    \/\/ nullableString \u306f\u3053\u306e else \u6587\u306e\u4e2d\u3067\u306f null \u578b\u3067\u306f\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u306a\u306e\u3067 string \u578b\u89e3\u91c8\u3059\u308b\n    nullableString.length \/\/ string \u78ba\u5b9a\u306a\u306e\u3067 length \u304c\u53c2\u7167\u3067\u304d\u308b\n    console.log(&quot;nullableString is string&quot;)\n  }\n}<\/code><\/pre>\n<p>null \u3084 undefined \u30c1\u30a7\u30c3\u30af\u306f\u3088\u304f\u4f7f\u3044\u307e\u3059\u3088\u306d\u3002<br \/>\n\u3082\u3061\u308d\u3093 null \u307f\u305f\u3044\u306a\u30d7\u30ea\u30df\u30c6\u30a3\u30d6\u578b\u3060\u3051\u3067\u306a\u304f\u30e6\u30fc\u30b6\u304c\u5b9a\u7fa9\u3057\u305f\u30aa\u30ea\u30b8\u30ca\u30eb\u306e\u578b\u306a\u3069\u3092\u5224\u5225\u3057\u305f\u3044\u3053\u3068\u306f\u5f80\u3005\u306b\u3057\u3066\u3042\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u305d\u306e\u6642\u306b\u767b\u5834\u3059\u308b\u306e\u304c is \u6f14\u7b97\u5b50\u3067\u3059\u3002<\/p>\n<h1>is \u3068\u306f\uff1f<\/h1>\n<h2>\u4f8b<\/h2>\n<p>\u8aac\u660e\u3059\u308b\u3088\u308a\u3082\u898b\u305f\u65b9\u304c\u65e9\u3044\u3068\u601d\u3046\u306e\u3067\u5148\u306b\u5b9f\u969b\u306e\u4f8b\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<br \/>\n\u5272\u3068\u5e83\u304f\u4f7f\u308f\u308c\u3066\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u4e00\u3064\u3067\u3042\u308b <a href=\"https:\/\/github.com\/axios\/axios#\">Axios<\/a> \u306b <code>isAxiosError<\/code> \u3068\u3044\u3046\u7c21\u5358\u306a\u3082\u306e\u304c\u5b9f\u88c5\u3055\u308c\u3066\u3044\u307e\u3059\u3002<br \/>\n\u4f7f\u3044\u65b9\u306f <a href=\"https:\/\/github.com\/axios\/axios#typescript\">\u4f8b<\/a> \u306b\u3082\u3042\u308b\u901a\u308a\u3001\u4f55\u304b\u3057\u3089\u306e\u5909\u6570\u304c AxiosError \u306a\u306e\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u304b\u3064\u578b\u30ac\u30fc\u30c9\u306b\u3088\u308a\u578b\u306e\u7d5e\u308a\u8fbc\u307f\u3092\u3084\u3063\u3066\u304f\u308c\u307e\u3059\u3002<br \/>\n\u305d\u306e\u307e\u307e\u7d39\u4ecb\u3057\u3088\u3046\u3068\u601d\u3063\u305f\u306e\u3067\u3059\u304c\u306a\u3093\u3068\u5143\u306e\u30b3\u30fc\u30c9\u306f JavaScript \u3060\u3063\u305f\u306e\u3067\u6bd4\u8f03\u7684\u8aad\u307f\u3084\u3059\u3044\u3088\u3046\u3053\u3061\u3089\u3067 TypeScript \u306b\u66f8\u304d\u76f4\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-typescript\">\/\/ https:\/\/github.com\/axios\/axios\/blob\/56e9ca1a865099f75eb0e897e944883a36bddf48\/lib\/utils.js#L112\nconst isObject = (thing: any) =&gt; thing !== null &amp;&amp; typeof thing === &#039;object&#039;;\n\n\/\/ https:\/\/github.com\/axios\/axios\/blob\/1e58a659ec9b0653f4693508d748caa5a41bb1a2\/index.d.cts#L74\nclass AxiosError&lt;T = unknown, D = any&gt; extends Error {\n  \/\/ \u3044\u308d\u3044\u308d\u30d7\u30ed\u30d1\u30c6\u30a3\u3042\u308b\u304c\u5272\u611b\n}\n\n\/\/ https:\/\/github.com\/axios\/axios\/blob\/56e9ca1a865099f75eb0e897e944883a36bddf48\/lib\/helpers\/isAxiosError.js#L12-14\n\/\/ https:\/\/github.com\/axios\/axios\/blob\/1e58a659ec9b0653f4693508d748caa5a41bb1a2\/index.d.cts#L485\n\/\/ \u672c\u4f53\u306f\u3053\u308c\nfunction isAxiosError&lt;T = any, D = any&gt;(payload: any): payload is AxiosError&lt;T, D&gt; {\n  return isObject(payload) &amp;&amp; (payload.isAxiosError === true);\n}<\/code><\/pre>\n<h3>isObject<\/h3>\n<p>\u3053\u308c\u3082\u5b9f\u8cea\u578b\u30ac\u30fc\u30c9\u307f\u305f\u3044\u306a\u3082\u306e\u3067\u3059\u306d is \u6f14\u7b97\u5b50\u304c\u4f7f\u308f\u308c\u3066\u306a\u3044\u306e\u3067\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30e9\u7684\u306b\u578b\u30ac\u30fc\u30c9\u306e\u578b\u7d5e\u308a\u8fbc\u307f\u306f\u884c\u3063\u3066\u304f\u308c\u307e\u305b\u3093\u304c null \u3067\u306f\u306a\u3044\u304b\u3064 <code>typeof<\/code> \u3067 object \u578b\u3067\u3042\u308b\u3053\u3068\u3092\u4fdd\u8a3c\u3057\u3066\u304f\u308c\u307e\u3059\u3002<\/p>\n<h3>isAxiosError<\/h3>\n<p>\u672c\u984c\u306f\u3053\u3061\u3089\u3067\u3059\u3002\u307e\u305a\u4e2d\u8eab\u3092\u898b\u3066\u307f\u308b\u3068<\/p>\n<pre><code class=\"language-typescript\">return isObject(payload) &amp;&amp; (payload.isAxiosError === true);<\/code><\/pre>\n<p>\u3068\u3042\u308a\u3001\u307e\u305a <code>payload<\/code> \u304c object \u3067\u3042\u308b\u3053\u3068\u3002\u3053\u308c\u306f\u3044\u3044\u3067\u3059\u306d\u3002<br \/>\n\u305d\u3057\u3066\u305d\u306e <code>payload<\/code> \u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u306b <code>isAxiosError<\/code> \u304c <code>true<\/code> \u3068\u3044\u3046\u5024\u304c\u5165\u3063\u3066\u3044\u308b\u3053\u3068\u3092\u898b\u3066\u3044\u307e\u3059\u3002<br \/>\n\u3053\u308c\u3060\u3051\u3067\u3059\u3002<br \/>\nAxios \u304c\u767a\u884c\u3059\u308b\u30a8\u30e9\u30fc\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u5fc5\u305a <code>true<\/code> \u304c\u5165\u3063\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u307f\u305f\u3044\u3067\u3059\u306d\u3002<\/p>\n<pre><code class=\"language-typescript\">function isAxiosError&lt;T = any, D = any&gt;(payload: any): payload is AxiosError&lt;T, D&gt;<\/code><\/pre>\n<p>\u95a2\u6570\u306e\u5b9a\u7fa9\u306f\u3053\u3046\u306a\u3063\u3066\u304a\u308a\u3001\u8fd4\u308a\u5024\u306e\u578b\u306b\u6ce8\u76ee\u3057\u3066\u307b\u3057\u3044\u306e\u3067\u3059\u304c\u3001\u3053\u308c\u306f\u3000 <code>isAxiosError<\/code> \u3068\u3044\u3046\u95a2\u6570\u304c <code>true<\/code> \u3092\u8fd4\u3059\u3068 \u5f15\u6570\u3068\u3057\u3066\u4e0e\u3048\u3089\u308c\u305f<code>payload<\/code> \u306f(is) <code>AxiosError&lt;T, D&gt;<\/code> \u578b\u3060\u3068 TypeScript \u306e\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30e9\u306b\u4f1d\u3048\u3066\u3042\u3052\u308b\u3068\u3044\u3046\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h2>\u8aac\u660e<\/h2>\n<p>\u4e0a\u8a18\u3067\u8aac\u660e\u3057\u305f\u3053\u3068\u304c\u307b\u307c\u5168\u3066\u3067\u306f\u3042\u308a\u307e\u3059\u304c\u3001\u4ee5\u4e0b URL \u306b\u8a73\u3057\u3044\u3061\u3083\u3093\u3068\u3057\u305f\u4ed5\u69d8\u304c\u66f8\u3044\u3066\u3042\u308b\u306e\u3067\u8ff7\u3063\u305f\u6642\u3084\u3001\u3082\u3063\u3068\u6df1\u304f\u77e5\u308a\u305f\u3044\u969b\u306b\u306f\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<ul>\n<li><a href=\"https:\/\/typescript-jp.gitbook.io\/deep-dive\/type-system\/typeguard#yznotype-guard\">https:\/\/typescript-jp.gitbook.io\/deep-dive\/type-system\/typeguard#yznotype-guard<\/a><\/li>\n<li><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/narrowing.html#using-type-predicates\">https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/narrowing.html#using-type-predicates<\/a><\/li>\n<\/ul>\n<h1>string \u914d\u5217\u5224\u5b9a\u306e\u95a2\u6570\u3092\u4f5c\u3063\u3066\u307f\u305f<\/h1>\n<p>\u307f\u306a\u3055\u3093 is \u6f14\u7b97\u5b50\u306b\u3064\u3044\u3066\u306f\u7406\u89e3\u3044\u305f\u3060\u3051\u305f\u3067\u3057\u3087\u3046\u304b\u3002<br \/>\n\u3053\u3053\u3067\u306f\u5b9f\u969b\u306b\u696d\u52d9\u3067 TypeScript \u3092\u66f8\u3044\u3066\u304a\u308a\u3001\u3068\u3042\u308b\u5909\u6570\u304c string \u306e\u914d\u5217\u306a\u306e\u304b\u3069\u3046\u304b\u3092\u6b63\u78ba\u306b\u77e5\u308a\u305f\u304f\u306a\u3063\u305f\u6642\u306b\u4f5c\u6210\u3057\u305f\u95a2\u6570\u306e\u7d39\u4ecb\u3092\u3057\u307e\u3059\u3002<\/p>\n<h2>\u4f5c\u6210\u3057\u305f\u95a2\u6570<\/h2>\n<pre><code class=\"language-typescript\">export const isStringArray = (value: unknown): value is string[] =&gt; {\n  return Array.isArray(value) &amp;&amp; value.every((v) =&gt; typeof v === &#039;string&#039;)\n}<\/code><\/pre>\n<p>\u4e0a\u306e\u8aac\u660e\u3092\u8aad\u3093\u3067\u304d\u305f\u65b9\u306a\u3089\u3082\u3046\u8aad\u3081\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u8aac\u660e\u3092\u3059\u308b\u3068 <code>value<\/code> \u3068\u3057\u3066\u53d7\u3051\u53d6\u3063\u305f\u3082\u306e\u304c\u3001<code>isArray<\/code> \u306b\u3066\u914d\u5217\u3067\u3042\u308b\u3053\u3068\u304b\u3064\u3001<br \/>\n\u914d\u5217\u3067\u3042\u308b\u3053\u3068\u304c\u78ba\u5b9a\u3057\u305f <code>value<\/code> \u3092 <code>every<\/code> \u95a2\u6570\u306b\u3088\u3063\u3066\u3059\u3079\u3066\u306e\u8981\u7d20\u304c <code>string<\/code> \u578b\u3067\u3042\u308c\u3070 <code>string[]<\/code> \u578b\u3067\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u3092\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30e9\u306b\u6559\u3048\u3066\u3042\u3052\u3066\u3044\u307e\u3059\u3002<br \/>\n\u5b9f\u969b\u306e\u4f7f\u3044\u3069\u3053\u308d\u3068\u3057\u3066\u306f\u3001<code>express<\/code> \u3067 Request \u304b\u3089 query \u3092\u53d7\u3051\u53d6\u308b\u3068 query \u306e\u578b\u306f <code>string | string[] | QueryString.ParsedQs | QueryString.ParsedQs[] | undefined<\/code> \u3068\u3044\u3046\u3042\u3089\u3086\u308b\u53ef\u80fd\u6027\u304c\u8003\u616e\u3055\u308c\u305f\u578b\u3068\u306a\u308a\u307e\u3059\u3002<br \/>\n\u305f\u3060\u306e <code>string<\/code> \u304c\u6b32\u3057\u3044\u306e\u3067\u3042\u308c\u3070 <code>typeof<\/code> \u3067\u3044\u3044\u306e\u3067\u3059\u304c\u3001<code>string[]<\/code> \u3092\u78ba\u5b9a\u3055\u305b\u3088\u3046\u3068\u3059\u308b\u3068 <code>isArray<\/code> \u3060\u3051\u3067\u306f\u4e0d\u5341\u5206\u306a\u306e\u3067\u81ea\u4f5c\u306e\u578b\u30ac\u30fc\u30c9\u3092\u4f5c\u6210\u3057\u305f\u3068\u3044\u3046\u7d4c\u7def\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h1>\u6c17\u3092\u3064\u3051\u306a\u3044\u3068\u578b\u306e\u507d\u308a\u306b\u306a\u308b\u3068\u3044\u3046\u8a71<\/h1>\n<p>is \u6f14\u7b97\u5b50\u3092\u4f7f\u3063\u305f\u578b\u5224\u5b9a\u307f\u306a\u3055\u3093\u3082\u4f5c\u3063\u3066\u307f\u305f\u304f\u306a\u3063\u305f\u3053\u3068\u3067\u3057\u3087\u3046\u3002\u3057\u304b\u3057 \u5b9a\u7fa9\u306b <code>is Hoge<\/code> \u3068\u66f8\u3044\u3066\u95a2\u6570\u304c true \u3055\u3048\u8fd4\u3057\u3066\u3057\u307e\u3048\u3070\u3082\u3046\u30c8\u30e9\u30f3\u30b9\u30d1\u30a4\u30e9\u306e\u4e2d\u3067\u306f\u305d\u308c\u306f Hoge \u578b\u3068\u3044\u3046\u3088\u3046\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u9069\u5f53\u306a\u5224\u5225\u3092\u66f8\u304f\u3068\u578b\u306e\u507d\u308a\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<br \/>\n<code>isAxiosError<\/code> \u306e\u5224\u5b9a\u304c\u6c7a\u3057\u3066\u9069\u5f53\u3068\u3044\u3046\u8a33\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u9069\u5f53\u306a\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b <code>isAxiosError<\/code> \u3068\u3044\u3046\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u6301\u305f\u305b\u3066\u305d\u3053\u306b true \u3092\u5165\u308c\u3066\u3057\u307e\u3048\u3070 <code>AxiosError<\/code> \u578b\u3067\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002\u95a2\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u5074\u3082\u4f7f\u3046\u5074\u3082\u3053\u3046\u3044\u3063\u305f\u5371\u967a\u6027\u304c\u3042\u308b\u3053\u3068\u306b\u306f\u7559\u610f\u3057\u3066\u304a\u3044\u305f\u65b9\u304c\u826f\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h1>\u7d42\u308f\u308a\u306b<\/h1>\n<p>as \u3084 any \u306b\u9003\u3052\u305a\u3088\u308a\u578b\u5b89\u5168\u3067\u5805\u7262\u306a\u30b7\u30b9\u30c6\u30e0\u3092\u4f5c\u308d\u3046\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u306f\u3058\u3081\u306b \u3053\u3093\u306b\u3061\u306f WESEEK \u3067\u308f\u308a\u3068\u4f55\u3067\u3082\u3084\u3063\u3066\u3044\u308b haruhikonyan \u3067\u3059\u3002 \u307f\u306a\u3055\u3093 TypeScript \u66f8\u3044\u3066\u307e\u3059\u304b\uff1f \u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u306f\u3082\u3061\u308d\u3093 Node \u3067\u30b5\u30fc\u30d0\u30b5\u30a4\u30c9\u3092\u66f8\u3044\u3066\u3082\u3088\u3057\u3001\u3055\u3089\u306b\u578b\u5b89<\/p>\n","protected":false},"author":3,"featured_media":3726,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[57],"tags":[],"_links":{"self":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/3958"}],"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=3958"}],"version-history":[{"count":11,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/3958\/revisions"}],"predecessor-version":[{"id":3970,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/posts\/3958\/revisions\/3970"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media\/3726"}],"wp:attachment":[{"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/media?parent=3958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/categories?post=3958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/weseek.co.jp\/tech\/wp-json\/wp\/v2\/tags?post=3958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}