{"id":4812,"date":"2023-01-05T10:29:16","date_gmt":"2023-01-05T02:29:16","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4812"},"modified":"2023-01-05T10:29:16","modified_gmt":"2023-01-05T02:29:16","slug":"%e4%ba%86%e8%a7%a3typescript%e5%87%bd%e6%95%b8%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5%e8%88%87%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3typescript%e5%87%bd%e6%95%b8%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5%e8%88%87%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u4e86\u89e3TypeScript\u51fd\u6578\u7684\u57fa\u672c\u6982\u5ff5\u8207\u529f\u80fd"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, Function\"><\/p>\n<h1>TypeScript \u51fd\u6578(Function) \u4ecb\u7d39<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u64c1\u6709 JavaScript \u7684\u6240\u6709\u529f\u80fd\uff0c\u4e26\u4e14\u64f4\u5145\u4e86\u4e00\u4e9b\u65b0\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u51fd\u6578(Function)\u3002\u51fd\u6578\u662f TypeScript \u7a0b\u5f0f\u8a2d\u8a08\u4e2d\u6700\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u5c07\u7a0b\u5f0f\u78bc\u5206\u5272\u6210\u53ef\u91cd\u8907\u4f7f\u7528\u7684\u584a\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5728 TypeScript \u4e2d\uff0c\u51fd\u6578\u53ef\u4ee5\u6709\u53c3\u6578\u548c\u56de\u50b3\u503c\uff0c\u53c3\u6578\u53ef\u4ee5\u662f\u4efb\u4f55\u985e\u578b\uff0c\u800c\u56de\u50b3\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u985e\u578b\uff0c\u6216\u8005\u662f void\uff0c\u8868\u793a\u6c92\u6709\u56de\u50b3\u503c\u3002\u51fd\u6578\u53ef\u4ee5\u662f\u540c\u6b65\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f\u975e\u540c\u6b65\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528 async \u548c await \u95dc\u9375\u5b57\u4f86\u5be6\u73fe\u975e\u540c\u6b65\u51fd\u6578\u3002<\/p>\n<h2>TypeScript \u51fd\u6578\u7684\u5b9a\u7fa9<\/h2>\n<p>\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 function \u95dc\u9375\u5b57\u4f86\u5b9a\u7fa9\u51fd\u6578\uff0c\u51fd\u6578\u7684\u5b9a\u7fa9\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction \u51fd\u6578\u540d\u7a31(\u53c3\u65781: \u53c3\u65781\u985e\u578b, \u53c3\u65782: \u53c3\u65782\u985e\u578b, ...): \u56de\u50b3\u503c\u985e\u578b {\n    \/\/ \u51fd\u6578\u5167\u5bb9\n    return \u56de\u50b3\u503c;\n}\n<\/pre>\n<p>\u51fd\u6578\u7684\u5b9a\u7fa9\u4e2d\uff0c\u53ef\u4ee5\u6307\u5b9a\u53c3\u6578\u7684\u985e\u578b\u548c\u56de\u50b3\u503c\u7684\u985e\u578b\uff0c\u5982\u679c\u6c92\u6709\u6307\u5b9a\uff0c\u5247\u53c3\u6578\u548c\u56de\u50b3\u503c\u7684\u985e\u578b\u90fd\u662f any\u3002<\/p>\n<h2>TypeScript \u51fd\u6578\u7684\u4f7f\u7528<\/h2>\n<p>\u5b9a\u7fa9\u5b8c\u51fd\u6578\u4e4b\u5f8c\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u51fd\u6578\u4e86\uff0c\u51fd\u6578\u7684\u4f7f\u7528\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: typescript\">\n\u51fd\u6578\u540d\u7a31(\u53c3\u65781, \u53c3\u65782, ...);\n<\/pre>\n<p>\u51fd\u6578\u7684\u4f7f\u7528\u4e2d\uff0c\u53c3\u6578\u7684\u985e\u578b\u5fc5\u9808\u548c\u5b9a\u7fa9\u6642\u4e00\u81f4\uff0c\u5426\u5247\u6703\u51fa\u73fe\u932f\u8aa4\u3002<\/p>\n<h2>TypeScript \u51fd\u6578\u7684\u7bc4\u4f8b<\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u500b\u7c21\u55ae\u7684 TypeScript \u51fd\u6578\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction add(x: number, y: number): number {\n    return x + y;\n}\n\nlet result = add(1, 2);\nconsole.log(result); \/\/ 3\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba add \u7684\u51fd\u6578\uff0c\u5b83\u63a5\u53d7\u5169\u500b\u53c3\u6578\uff0c\u90fd\u662f number \u985e\u578b\uff0c\u4e26\u4e14\u56de\u50b3\u4e00\u500b number \u985e\u578b\u7684\u503c\u3002\u7136\u5f8c\u6211\u5011\u4f7f\u7528\u51fd\u6578\uff0c\u50b3\u5165\u5169\u500b\u53c3\u6578 1 \u548c 2\uff0c\u6700\u5f8c\u5f97\u5230\u7684\u7d50\u679c\u662f 3\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u64c1\u6709 JavaScript \u7684\u6240\u6709\u529f\u80fd\uff0c\u4e26\u4e14\u64f4\u5145\u4e86\u4e00\u4e9b\u65b0\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u51fd\u6578(Function)\u3002\u51fd\u6578\u662f TypeScript \u7a0b\u5f0f\u8a2d\u8a08\u4e2d\u6700\u91cd\u8981\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u5c07\u7a0b\u5f0f\u78bc\u5206\u5272\u6210\u53ef\u91cd\u8907\u4f7f\u7528\u7684\u584a\uff0c\u4ee5\u63d0\u9ad8\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 function \u95dc\u9375\u5b57\u4f86\u5b9a\u7fa9\u51fd\u6578\uff0c\u4e26\u4e14\u53ef\u4ee5\u6307\u5b9a\u53c3\u6578\u7684\u985e\u578b\u548c\u56de\u50b3\u503c\u7684\u985e\u578b\uff0c\u5b9a\u7fa9\u5b8c\u51fd\u6578\u4e4b\u5f8c\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528\u51fd\u6578\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981(Description):\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39TypeScript\u51fd\u6578\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4e26\u8a73\u7d30\u89e3\u91cb\u5b83\u5011\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u63d0\u9ad8\u7a0b\u5e8f\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-4812","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-1fC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4812","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=4812"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4812\/revisions"}],"predecessor-version":[{"id":4813,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4812\/revisions\/4813"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}