{"id":5204,"date":"2023-01-05T12:11:56","date_gmt":"2023-01-05T04:11:56","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5204"},"modified":"2023-01-05T12:11:56","modified_gmt":"2023-01-05T04:11:56","slug":"%e4%ba%86%e8%a7%a3%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8typescript%e6%b3%9b%e5%9e%8bgenerics%e7%9a%84%e5%a4%9a%e7%a8%ae%e9%a1%9e%e5%9e%8b%e5%8f%83%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8typescript%e6%b3%9b%e5%9e%8bgenerics%e7%9a%84%e5%a4%9a%e7%a8%ae%e9%a1%9e%e5%9e%8b%e5%8f%83%e6%95%b8\/","title":{"rendered":"\u4e86\u89e3\u5982\u4f55\u5229\u7528TypeScript\u6cdb\u578b(Generics)\u7684\u591a\u7a2e\u985e\u578b\u53c3\u6578"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u6cdb\u578b, generics, \u591a\u7c7b\u578b\u53c2\u6570\"><\/p>\n<h1>TypeScript \u6cdb\u578b(generics)\u7684\u591a\u7c7b\u578b\u53c2\u6570<\/h1>\n<p>TypeScript \u6cdb\u578b(generics)\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7de8\u5beb\u7a0b\u5f0f\u6642\uff0c\u66f4\u52a0\u6709\u6548\u7387\u5730\u4f7f\u7528\u591a\u7a2e\u985e\u578b\u7684\u53c3\u6578\u3002<\/p>\n<p>\u6cdb\u578b\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u5b9a\u7fa9\u51fd\u5f0f\u6642\uff0c\u6307\u5b9a\u4e00\u500b\u6216\u591a\u500b\u53c3\u6578\u7684\u985e\u578b\uff0c\u800c\u4e0d\u9700\u8981\u5728\u51fd\u5f0f\u5167\u90e8\u6307\u5b9a\u6bcf\u500b\u53c3\u6578\u7684\u985e\u578b\u3002\u9019\u6a23\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u52a0\u6709\u6548\u7387\u5730\u7de8\u5beb\u7a0b\u5f0f\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u64f4\u5145\u51fd\u5f0f\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u60f3\u8981\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f\uff0c\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u985e\u578b\u7684\u53c3\u6578\uff0c\u4e26\u4e14\u5c07\u53c3\u6578\u7684\u503c\u52a0\u4e0a\u4e00\u500b\u6578\u5b57\uff0c\u7136\u5f8c\u56de\u50b3\u7d50\u679c\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction add(a: any, b: any): any {\n  return a + b;\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u51fd\u5f0f\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u985e\u578b\u7684\u53c3\u6578\uff0c\u4f46\u662f\u5b83\u4e0d\u80fd\u6aa2\u67e5\u53c3\u6578\u7684\u985e\u578b\uff0c\u4e5f\u4e0d\u80fd\u6aa2\u67e5\u51fd\u5f0f\u7684\u7d50\u679c\u662f\u5426\u6b63\u78ba\u3002<\/p>\n<p>\u4f7f\u7528\u6cdb\u578b\uff0c\u6211\u5011\u53ef\u4ee5\u6539\u5beb\u4e0a\u9762\u7684\u51fd\u5f0f\uff0c\u8b93\u5b83\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u985e\u578b\u7684\u53c3\u6578\uff0c\u4e26\u4e14\u53ef\u4ee5\u6aa2\u67e5\u53c3\u6578\u7684\u985e\u578b\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction add<T>(a: T, b: T): T {\n  return a + b;\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u51fd\u5f0f\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u985e\u578b\u7684\u53c3\u6578\uff0c\u4e26\u4e14\u53ef\u4ee5\u6aa2\u67e5\u53c3\u6578\u7684\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u51fd\u5f0f\u7684\u7d50\u679c\u662f\u6b63\u78ba\u7684\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u6cdb\u578b\u9084\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u52a0\u6709\u6548\u7387\u5730\u64f4\u5145\u51fd\u5f0f\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u4e0a\u9762\u7684\u51fd\u5f0f\u64f4\u5145\u70ba\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6578\u91cf\u7684\u53c3\u6578\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction add<T>(...args: T[]): T {\n  let result = args[0];\n  for (let i = 1; i < args.length; i++) {\n    result = result + args[i];\n  }\n  return result;\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u51fd\u5f0f\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6578\u91cf\u7684\u53c3\u6578\uff0c\u4e26\u4e14\u53ef\u4ee5\u6aa2\u67e5\u53c3\u6578\u7684\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u51fd\u5f0f\u7684\u7d50\u679c\u662f\u6b63\u78ba\u7684\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cTypeScript \u6cdb\u578b(generics)\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7de8\u5beb\u7a0b\u5f0f\u6642\uff0c\u66f4\u52a0\u6709\u6548\u7387\u5730\u4f7f\u7528\u591a\u7a2e\u985e\u578b\u7684\u53c3\u6578\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u52a0\u6709\u6548\u7387\u5730\u7de8\u5beb\u7a0b\u5f0f\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u64f4\u5145\u51fd\u5f0f\u7684\u529f\u80fd\u3002<\/p>\n<p><!--more--><\/p>\n<h2>TypeScript \u6cdb\u578b\u7684\u512a\u9ede<\/h2>\n<p>TypeScript \u6cdb\u578b\u7684\u6700\u5927\u512a\u9ede\u662f\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u52a0\u6709\u6548\u7387\u5730\u7de8\u5beb\u7a0b\u5f0f\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u64f4\u5145\u51fd\u5f0f\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4f7f\u7528\u6cdb\u578b\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5728\u5b9a\u7fa9\u51fd\u5f0f\u6642\uff0c\u6307\u5b9a\u4e00\u500b\u6216\u591a\u500b\u53c3\u6578\u7684\u985e\u578b\uff0c\u800c\u4e0d\u9700\u8981\u5728\u51fd\u5f0f\u5167\u90e8\u6307\u5b9a\u6bcf\u500b\u53c3\u6578\u7684\u985e\u578b\u3002\u9019\u6a23\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u52a0\u6709\u6548\u7387\u5730\u7de8\u5beb\u7a0b\u5f0f\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u64f4\u5145\u51fd\u5f0f\u7684\u529f\u80fd\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u6cdb\u578b\u9084\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u52a0\u6709\u6548\u7387\u5730\u6aa2\u67e5\u53c3\u6578\u7684\u985e\u578b\uff0c\u4ee5\u78ba\u4fdd\u51fd\u5f0f\u7684\u7d50\u679c\u662f\u6b63\u78ba\u7684\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>TypeScript \u6cdb\u578b(generics)\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7de8\u5beb\u7a0b\u5f0f\u6642\uff0c\u66f4\u52a0\u6709\u6548\u7387\u5730\u4f7f\u7528\u591a\u7a2e\u985e\u578b\u7684\u53c3\u6578\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u52a0\u6709\u6548\u7387\u5730\u7de8\u5beb\u7a0b\u5f0f\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u64f4\u5145\u51fd\u5f0f\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):\u4e86\u89e3\u5982\u4f55\u5229\u7528TypeScript\u6cdb\u578b(Generics)\u7684\u591a\u7a2e\u985e\u578b\u53c3\u6578\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u6709\u6548\u7387\u5730\u64b0\u5beb\u7a0b\u5f0f\u78bc\uff0c\u4e26\u63d0\u9ad8\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002\u672c\u6587\u5c07\u4ecb\u7d39TypeScript\u6cdb\u578b\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u6539\u5584\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\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-5204","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-1lW","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5204","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=5204"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5204\/revisions"}],"predecessor-version":[{"id":5205,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5204\/revisions\/5205"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}