{"id":5226,"date":"2023-01-05T12:14:28","date_gmt":"2023-01-05T04:14:28","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5226"},"modified":"2023-01-05T12:14:28","modified_gmt":"2023-01-05T04:14:28","slug":"%e4%ba%86%e8%a7%a3%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e6%b3%9b%e5%9e%8b%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e6%b3%9b%e5%9e%8b%e6%8e%a5%e5%8f%a3\/","title":{"rendered":"\u4e86\u89e3\u5982\u4f55\u4f7f\u7528TypeScript\u6cdb\u578b\u63a5\u53e3"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u6cdb\u578b, \u6cdb\u578b\u63a5\u53e3\"><\/p>\n<h1>TypeScript \u6cdb\u578b(generics)\u7684\u6cdb\u578b\u63a5\u53e3<\/h1>\n<p>TypeScript \u6cdb\u578b(generics)\u7684\u6cdb\u578b\u63a5\u53e3\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u69cb\u5efa\u53ef\u91cd\u7528\u7684\u4ee3\u78bc\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u69cb\u5efa\u53ef\u91cd\u7528\u7684\u4ee3\u78bc\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u63a7\u5236\u8cc7\u6599\u985e\u578b\u3002<\/p>\n<p>\u6cdb\u578b\u63a5\u53e3\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5b9a\u7fa9\u51fd\u6578\u548c\u985e\u5225\u7684\u53c3\u6578\u548c\u5c6c\u6027\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u63a7\u5236\u8cc7\u6599\u985e\u578b\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5b9a\u7fa9\u51fd\u6578\u548c\u985e\u5225\u7684\u53c3\u6578\u548c\u5c6c\u6027\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u63a7\u5236\u8cc7\u6599\u985e\u578b\u3002<\/p>\n<p>TypeScript \u6cdb\u578b\u63a5\u53e3\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: typescript\">\ninterface GenericInterface<T> {\n    (arg: T): T;\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u78bc\u5b9a\u7fa9\u4e86\u4e00\u500b\u6cdb\u578b\u63a5\u53e3\uff0c\u5b83\u63a5\u53d7\u4e00\u500b\u53c3\u6578\uff0c\u4e26\u4e14\u8fd4\u56de\u4e00\u500b\u76f8\u540c\u985e\u578b\u7684\u53c3\u6578\u3002<\/p>\n<p>\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u6cdb\u578b\u63a5\u53e3\u4f86\u5b9a\u7fa9\u4e00\u500b\u51fd\u6578\uff0c\u8a72\u51fd\u6578\u63a5\u53d7\u4e00\u500b\u53c3\u6578\uff0c\u4e26\u4e14\u8fd4\u56de\u4e00\u500b\u76f8\u540c\u985e\u578b\u7684\u53c3\u6578\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet myGenericFunction: GenericInterface<number> = function (arg: number) {\n    return arg;\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u78bc\u5b9a\u7fa9\u4e86\u4e00\u500b\u51fd\u6578\uff0c\u8a72\u51fd\u6578\u63a5\u53d7\u4e00\u500b\u6578\u5b57\u53c3\u6578\uff0c\u4e26\u4e14\u8fd4\u56de\u4e00\u500b\u6578\u5b57\u53c3\u6578\u3002<\/p>\n<p>\u6211\u5011\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6cdb\u578b\u63a5\u53e3\u4f86\u5b9a\u7fa9\u4e00\u500b\u985e\u5225\uff0c\u8a72\u985e\u5225\u63a5\u53d7\u4e00\u500b\u53c3\u6578\uff0c\u4e26\u4e14\u8fd4\u56de\u4e00\u500b\u76f8\u540c\u985e\u578b\u7684\u53c3\u6578\uff1a<\/p>\n<pre class=\"brush: typescript\">\nclass MyGenericClass<T> {\n    value: T;\n    getValue(): T {\n        return this.value;\n    }\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u78bc\u5b9a\u7fa9\u4e86\u4e00\u500b\u985e\u5225\uff0c\u8a72\u985e\u5225\u63a5\u53d7\u4e00\u500b\u53c3\u6578\uff0c\u4e26\u4e14\u8fd4\u56de\u4e00\u500b\u76f8\u540c\u985e\u578b\u7684\u53c3\u6578\u3002<\/p>\n<p>TypeScript \u6cdb\u578b\u63a5\u53e3\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5b9a\u7fa9\u51fd\u6578\u548c\u985e\u5225\u7684\u53c3\u6578\u548c\u5c6c\u6027\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u63a7\u5236\u8cc7\u6599\u985e\u578b\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u69cb\u5efa\u53ef\u91cd\u7528\u7684\u4ee3\u78bc\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u63a7\u5236\u8cc7\u6599\u985e\u578b\u3002<\/p>\n<p><!--more--><\/p>\n<h2>TypeScript \u6cdb\u578b\u63a5\u53e3\u7684\u512a\u9ede<\/h2>\n<p>TypeScript \u6cdb\u578b\u63a5\u53e3\u7684\u512a\u9ede\u6709\uff1a<\/p>\n<ul>\n<li><strong>\u53ef\u91cd\u7528\u6027\uff1a<\/strong>\u6cdb\u578b\u63a5\u53e3\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u69cb\u5efa\u53ef\u91cd\u7528\u7684\u4ee3\u78bc\u3002<\/li>\n<li><strong>\u53ef\u64f4\u5c55\u6027\uff1a<\/strong>\u6cdb\u578b\u63a5\u53e3\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u64f4\u5c55\u51fd\u6578\u548c\u985e\u5225\u7684\u53c3\u6578\u548c\u5c6c\u6027\u3002<\/li>\n<li><strong>\u53ef\u63a7\u6027\uff1a<\/strong>\u6cdb\u578b\u63a5\u53e3\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u63a7\u5236\u8cc7\u6599\u985e\u578b\u3002<\/li>\n<\/ul>\n<h2>\u7e3d\u7d50<\/h2>\n<p>TypeScript \u6cdb\u578b\u63a5\u53e3\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u69cb\u5efa\u53ef\u91cd\u7528\u7684\u4ee3\u78bc\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u63a7\u5236\u8cc7\u6599\u985e\u578b\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5b9a\u7fa9\u51fd\u6578\u548c\u985e\u5225\u7684\u53c3\u6578\u548c\u5c6c\u6027\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u63a7\u5236\u8cc7\u6599\u985e\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:TypeScript\u6cdb\u578b\u63a5\u53e3\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u6709\u6548\u7387\u5730\u5b9a\u7fa9\u548c\u4f7f\u7528\u8907\u96dc\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528\u6cdb\u578b\u63a5\u53e3\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\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-5226","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-1mi","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5226","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=5226"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5226\/revisions"}],"predecessor-version":[{"id":5227,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5226\/revisions\/5227"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}