{"id":5072,"date":"2023-01-05T11:49:53","date_gmt":"2023-01-05T03:49:53","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5072"},"modified":"2023-01-05T11:49:53","modified_gmt":"2023-01-05T03:49:53","slug":"%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e6%95%b8%e7%b5%84%e7%9a%84%e9%a1%9e%e5%9e%8b%e5%ae%88%e8%a1%9b%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3typescript%e4%b8%ad%e6%95%b8%e7%b5%84%e7%9a%84%e9%a1%9e%e5%9e%8b%e5%ae%88%e8%a1%9b%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u4e86\u89e3TypeScript\u4e2d\u6578\u7d44\u7684\u985e\u578b\u5b88\u885b\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u6578\u7d44\u7684\u985e\u578b\u5b88\u885b, Array Type Guards\"><\/p>\n<h1>TypeScript \u6578\u7d44\u7684\u985e\u578b\u5b88\u885b(Array Type Guards)<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u985e\u578b\u7cfb\u7d71\uff0c\u53ef\u4ee5\u5728\u7de8\u8b6f\u6642\u671f\u6aa2\u67e5\u985e\u578b\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5f0f\u7684\u54c1\u8cea\u3002\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u300c\u6578\u7d44\u7684\u985e\u578b\u5b88\u885b(Array Type Guards)\u300d\u4f86\u6aa2\u67e5\u4e00\u500b\u8b8a\u6578\u662f\u5426\u70ba\u6578\u7d44\uff0c\u4ee5\u53ca\u6578\u7d44\u4e2d\u7684\u5143\u7d20\u662f\u4ec0\u9ebc\u985e\u578b\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u6578\u7d44\u7684\u985e\u578b\u5b88\u885b\u53ef\u4ee5\u4f7f\u7528 <code>Array.isArray()<\/code> \u65b9\u6cd5\u4f86\u6aa2\u67e5\u4e00\u500b\u8b8a\u6578\u662f\u5426\u70ba\u6578\u7d44\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: javascript\">\nlet arr = [1, 2, 3];\nif (Array.isArray(arr)) {\n    console.log('arr is an array');\n}\n<\/pre>\n<p>\u53e6\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>instanceof<\/code> \u64cd\u4f5c\u7b26\u4f86\u6aa2\u67e5\u4e00\u500b\u8b8a\u6578\u662f\u5426\u70ba\u6578\u7d44\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: javascript\">\nlet arr = [1, 2, 3];\nif (arr instanceof Array) {\n    console.log('arr is an array');\n}\n<\/pre>\n<p>\u6b64\u5916\uff0c\u9084\u53ef\u4ee5\u4f7f\u7528 <code>in<\/code> \u64cd\u4f5c\u7b26\u4f86\u6aa2\u67e5\u4e00\u500b\u8b8a\u6578\u662f\u5426\u70ba\u6578\u7d44\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: javascript\">\nlet arr = [1, 2, 3];\nif ('length' in arr) {\n    console.log('arr is an array');\n}\n<\/pre>\n<p>\u53e6\u5916\uff0c\u9084\u53ef\u4ee5\u4f7f\u7528 <code>typeof<\/code> \u64cd\u4f5c\u7b26\u4f86\u6aa2\u67e5\u4e00\u500b\u8b8a\u6578\u662f\u5426\u70ba\u6578\u7d44\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: javascript\">\nlet arr = [1, 2, 3];\nif (typeof arr === 'object' && arr.constructor === Array) {\n    console.log('arr is an array');\n}\n<\/pre>\n<p>\u6b64\u5916\uff0c\u9084\u53ef\u4ee5\u4f7f\u7528 <code>Object.prototype.toString.call()<\/code> \u65b9\u6cd5\u4f86\u6aa2\u67e5\u4e00\u500b\u8b8a\u6578\u662f\u5426\u70ba\u6578\u7d44\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: javascript\">\nlet arr = [1, 2, 3];\nif (Object.prototype.toString.call(arr) === '[object Array]') {\n    console.log('arr is an array');\n}\n<\/pre>\n<p>\u53e6\u5916\uff0c\u9084\u53ef\u4ee5\u4f7f\u7528 <code>Array.prototype.some()<\/code> \u65b9\u6cd5\u4f86\u6aa2\u67e5\u4e00\u500b\u8b8a\u6578\u662f\u5426\u70ba\u6578\u7d44\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: javascript\">\nlet arr = [1, 2, 3];\nif (arr.some(item => item instanceof Array)) {\n    console.log('arr is an array');\n}\n<\/pre>\n<p>\u6b64\u5916\uff0c\u9084\u53ef\u4ee5\u4f7f\u7528 <code>Array.prototype.every()<\/code> \u65b9\u6cd5\u4f86\u6aa2\u67e5\u4e00\u500b\u8b8a\u6578\u662f\u5426\u70ba\u6578\u7d44\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: javascript\">\nlet arr = [1, 2, 3];\nif (arr.every(item => typeof item === 'number')) {\n    console.log('arr is an array');\n}\n<\/pre>\n<p>\u4f7f\u7528\u6578\u7d44\u7684\u985e\u578b\u5b88\u885b\u53ef\u4ee5\u6aa2\u67e5\u4e00\u500b\u8b8a\u6578\u662f\u5426\u70ba\u6578\u7d44\uff0c\u4ee5\u53ca\u6578\u7d44\u4e2d\u7684\u5143\u7d20\u662f\u4ec0\u9ebc\u985e\u578b\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5f0f\u7684\u54c1\u8cea\uff0c\u4e26\u4e14\u53ef\u4ee5\u6e1b\u5c11\u51fa\u932f\u7684\u6a5f\u6703\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TypeScript\u4e2d\u7684\u6578\u7d44\u985e\u578b\u5b88\u885b\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u6aa2\u67e5\u548c\u64cd\u4f5c\u6578\u7d44\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528\u6578\u7d44\u985e\u578b\u5b88\u885b\u4f86\u63d0\u9ad8\u958b\u767c\u6548\u7387\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-5072","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1jO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5072"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5072\/revisions"}],"predecessor-version":[{"id":5073,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5072\/revisions\/5073"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}