{"id":5370,"date":"2023-01-05T12:32:50","date_gmt":"2023-01-05T04:32:50","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5370"},"modified":"2023-01-05T12:32:50","modified_gmt":"2023-01-05T04:32:50","slug":"%e5%ad%b8%e7%bf%92typescript%e4%b8%ad%e7%9a%84%e6%95%b8%e7%b5%84%e6%b1%82%e5%8d%94%e6%96%b9%e5%b7%ae-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92typescript%e4%b8%ad%e7%9a%84%e6%95%b8%e7%b5%84%e6%b1%82%e5%8d%94%e6%96%b9%e5%b7%ae-2\/","title":{"rendered":"\u5b78\u7fd2TypeScript\u4e2d\u7684\u6578\u7d44\u6c42\u5354\u65b9\u5dee"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u6578\u7d44, \u5354\u65b9\u5dee, Array Covariance\"><\/p>\n<h1>TypeScript \u6578\u7d44\u7684\u6c42\u5354\u65b9\u5dee(Array Covariance)<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u985e\u578b\u7cfb\u7d71\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7de8\u5beb\u7a0b\u5f0f\u6642\u66f4\u52a0\u5b89\u5168\u53ef\u9760\u3002\u5728 TypeScript \u4e2d\uff0c\u6578\u7d44\u7684\u6c42\u5354\u65b9\u5dee(Array Covariance)\u662f\u4e00\u500b\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528 TypeScript\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39 TypeScript \u4e2d\u7684\u6578\u7d44\u5354\u65b9\u5dee\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u7a0b\u5f0f\u7bc4\u4f8b\u4f86\u8aaa\u660e\u5982\u4f55\u4f7f\u7528\u5b83\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u6578\u7d44\u5354\u65b9\u5dee\u662f\u4e00\u7a2e\u7279\u6b8a\u7684\u985e\u578b\u8f49\u63db\uff0c\u5b83\u5141\u8a31\u958b\u767c\u8005\u5c07\u4e00\u500b\u985e\u578b\u7684\u6578\u7d44\u8f49\u63db\u70ba\u53e6\u4e00\u500b\u985e\u578b\u7684\u6578\u7d44\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b <span style=\"font-weight: bold;\">string<\/span> \u985e\u578b\u7684\u6578\u7d44\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet strings: string[] = [\"Hello\", \"World\"];\n<\/pre>\n<p>\u6211\u5011\u53ef\u4ee5\u5c07\u5b83\u8f49\u63db\u70ba <span style=\"font-weight: bold;\">number<\/span> \u985e\u578b\u7684\u6578\u7d44\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet numbers: number[] = strings.map(s => parseInt(s));\n<\/pre>\n<p>\u9019\u7a2e\u8f49\u63db\u7a31\u70ba\u6578\u7d44\u5354\u65b9\u5dee\uff0c\u5b83\u5141\u8a31\u958b\u767c\u8005\u5c07\u4e00\u500b\u985e\u578b\u7684\u6578\u7d44\u8f49\u63db\u70ba\u53e6\u4e00\u500b\u985e\u578b\u7684\u6578\u7d44\uff0c\u800c\u4e0d\u6703\u72af\u4e0b\u4efb\u4f55\u932f\u8aa4\u3002<\/p>\n<p>\u5728 TypeScript \u4e2d\uff0c\u6578\u7d44\u5354\u65b9\u5dee\u662f\u4e00\u7a2e\u7279\u6b8a\u7684\u985e\u578b\u8f49\u63db\uff0c\u5b83\u5141\u8a31\u958b\u767c\u8005\u5c07\u4e00\u500b\u985e\u578b\u7684\u6578\u7d44\u8f49\u63db\u70ba\u53e6\u4e00\u500b\u985e\u578b\u7684\u6578\u7d44\uff0c\u800c\u4e0d\u6703\u72af\u4e0b\u4efb\u4f55\u932f\u8aa4\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b <span style=\"font-weight: bold;\">string<\/span> \u985e\u578b\u7684\u6578\u7d44\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet strings: string[] = [\"Hello\", \"World\"];\n<\/pre>\n<p>\u6211\u5011\u53ef\u4ee5\u5c07\u5b83\u8f49\u63db\u70ba <span style=\"font-weight: bold;\">number<\/span> \u985e\u578b\u7684\u6578\u7d44\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet numbers: number[] = strings.map(s => parseInt(s));\n<\/pre>\n<p>\u9019\u7a2e\u8f49\u63db\u7a31\u70ba\u6578\u7d44\u5354\u65b9\u5dee\uff0c\u5b83\u5141\u8a31\u958b\u767c\u8005\u5c07\u4e00\u500b\u985e\u578b\u7684\u6578\u7d44\u8f49\u63db\u70ba\u53e6\u4e00\u500b\u985e\u578b\u7684\u6578\u7d44\uff0c\u800c\u4e0d\u6703\u72af\u4e0b\u4efb\u4f55\u932f\u8aa4\u3002<\/p>\n<p>\u6578\u7d44\u5354\u65b9\u5dee\u662f\u4e00\u7a2e\u975e\u5e38\u6709\u7528\u7684\u6280\u8853\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u5c0d\u6578\u7d44\u9032\u884c\u64cd\u4f5c\uff0c\u4e26\u66f4\u5bb9\u6613\u5730\u5c07\u6578\u64da\u5f9e\u4e00\u500b\u985e\u578b\u8f49\u63db\u70ba\u53e6\u4e00\u500b\u985e\u578b\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b <span style=\"font-weight: bold;\">Person<\/span> \u985e\u578b\u7684\u6578\u7d44\uff1a<\/p>\n<pre class=\"brush: typescript\">\nclass Person {\n    name: string;\n    age: number;\n}\n\nlet people: Person[] = [\n    { name: \"John\", age: 30 },\n    { name: \"Jane\", age: 20 }\n];\n<\/pre>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u6578\u7d44\u5354\u65b9\u5dee\u5c07\u5b83\u8f49\u63db\u70ba <span style=\"font-weight: bold;\">string<\/span> \u985e\u578b\u7684\u6578\u7d44\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet names: string[] = people.map(p => p.name);\n<\/pre>\n<p>\u9019\u6a23\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07 <span style=\"font-weight: bold;\">Person<\/span> \u985e\u578b\u7684\u6578\u7d44\u8f49\u63db\u70ba <span style=\"font-weight: bold;\">string<\/span> \u985e\u578b\u7684\u6578\u7d44\uff0c\u800c\u4e0d\u6703\u72af\u4e0b\u4efb\u4f55\u932f\u8aa4\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0c\u6578\u7d44\u5354\u65b9\u5dee\u662f TypeScript \u4e2d\u4e00\u500b\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u5c0d\u6578\u7d44\u9032\u884c\u64cd\u4f5c\uff0c\u4e26\u66f4\u5bb9\u6613\u5730\u5c07\u6578\u64da\u5f9e\u4e00\u500b\u985e\u578b\u8f49\u63db\u70ba\u53e6\u4e00\u500b\u985e\u578b\u3002\u672c\u6587\u8a73\u7d30\u4ecb\u7d39\u4e86 TypeScript \u4e2d\u7684\u6578\u7d44\u5354\u65b9\u5dee\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7a0b\u5f0f\u7bc4\u4f8b\u4f86\u8aaa\u660e\u5982\u4f55\u4f7f\u7528\u5b83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39TypeScript\u4e2d\u7684\u6578\u7d44\u6c42\u5354\u65b9\u5dee\uff0c\u8a73\u7d30\u89e3\u91cb\u5982\u4f55\u4f7f\u7528TypeScript\u4e2d\u7684\u65b9\u6cd5\u4f86\u6c42\u53d6\u6578\u7d44\u7684\u5354\u65b9\u5dee\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u793a\u4f8b\u4f86\u8a73\u7d30\u8aaa\u660e\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-5370","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-1oC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5370","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=5370"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5370\/revisions"}],"predecessor-version":[{"id":5371,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5370\/revisions\/5371"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}