{"id":5140,"date":"2023-01-05T12:02:05","date_gmt":"2023-01-05T04:02:05","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5140"},"modified":"2023-01-05T12:02:05","modified_gmt":"2023-01-05T04:02:05","slug":"%e4%ba%86%e8%a7%a3typescript%e5%87%bd%e6%95%b8%e5%8f%af%e9%80%89%e5%8f%83%e6%95%b8%e7%9a%84%e5%a8%81%e5%8a%9b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%ba%86%e8%a7%a3typescript%e5%87%bd%e6%95%b8%e5%8f%af%e9%80%89%e5%8f%83%e6%95%b8%e7%9a%84%e5%a8%81%e5%8a%9b\/","title":{"rendered":"\u4e86\u89e3TypeScript\u51fd\u6578\u53ef\u9009\u53c3\u6578\u7684\u5a01\u529b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u51fd\u6570, \u53ef\u9009\u53c2\u6570\"><\/p>\n<h1>TypeScript \u51fd\u6570(functions)\u7684\u53ef\u9009\u53c2\u6570<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u53ef\u4ee5\u4f7f\u7528\u53ef\u9009\u53c2\u6570\u3002\u53ef\u9009\u53c2\u6570\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u5b9a\u7fa9\u51fd\u6578\u6642\uff0c\u6307\u5b9a\u67d0\u4e9b\u53c3\u6578\u662f\u53ef\u9078\u7684\uff0c\u800c\u4e0d\u662f\u5fc5\u9808\u50b3\u5165\u7684\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u53ef\u9009\u53c2\u6578\u7684\u5b9a\u7fa9\u65b9\u5f0f\u5f88\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u5728\u53c3\u6578\u540d\u7a31\u5f8c\u9762\u52a0\u4e0a\u4e00\u500b <span style=\"color: #009900; font-weight: bold;\">?<\/span> \u5c31\u53ef\u4ee5\u4e86\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction add(x: number, y?: number): number {\n    if (y) {\n        return x + y;\n    } else {\n        return x;\n    }\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u51fd\u6578\u5b9a\u7fa9\u4e2d\uff0c<span style=\"color: #009900; font-weight: bold;\">y<\/span> \u662f\u4e00\u500b\u53ef\u9078\u53c3\u6578\uff0c\u5982\u679c\u6c92\u6709\u50b3\u5165 <span style=\"color: #009900; font-weight: bold;\">y<\/span> \u7684\u503c\uff0c\u5247\u51fd\u6578\u6703\u5ffd\u7565 <span style=\"color: #009900; font-weight: bold;\">y<\/span> \u7684\u503c\uff0c\u76f4\u63a5\u8fd4\u56de <span style=\"color: #009900; font-weight: bold;\">x<\/span> \u7684\u503c\u3002<\/p>\n<p>\u53ef\u9009\u53c3\u6578\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u5b9a\u7fa9\u51fd\u6578\u6642\uff0c\u66f4\u52a0\u7c21\u55ae\u7684\u5b9a\u7fa9\u51fd\u6578\u7684\u53c3\u6578\uff0c\u800c\u4e0d\u9700\u8981\u6bcf\u500b\u53c3\u6578\u90fd\u662f\u5fc5\u9808\u50b3\u5165\u7684\uff0c\u53ef\u4ee5\u66f4\u52a0\u5f48\u6027\u7684\u5b9a\u7fa9\u51fd\u6578\u7684\u53c3\u6578\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u53ef\u9009\u53c3\u6578\u4e5f\u53ef\u4ee5\u7528\u65bc\u51fd\u6578\u7684\u91cd\u8f09\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction add(x: number, y?: number): number;\nfunction add(x: string, y?: string): string;\n\nfunction add(x: any, y?: any): any {\n    if (y) {\n        return x + y;\n    } else {\n        return x;\n    }\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u51fd\u6578\u5b9a\u7fa9\u4e2d\uff0c<span style=\"color: #009900; font-weight: bold;\">y<\/span> \u662f\u4e00\u500b\u53ef\u9078\u53c3\u6578\uff0c\u5982\u679c\u6c92\u6709\u50b3\u5165 <span style=\"color: #009900; font-weight: bold;\">y<\/span> \u7684\u503c\uff0c\u5247\u51fd\u6578\u6703\u5ffd\u7565 <span style=\"color: #009900; font-weight: bold;\">y<\/span> \u7684\u503c\uff0c\u76f4\u63a5\u8fd4\u56de <span style=\"color: #009900; font-weight: bold;\">x<\/span> \u7684\u503c\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cTypeScript \u63d0\u4f9b\u4e86\u53ef\u9009\u53c3\u6578\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u52a0\u5f48\u6027\u7684\u5b9a\u7fa9\u51fd\u6578\u7684\u53c3\u6578\uff0c\u4e5f\u53ef\u4ee5\u7528\u65bc\u51fd\u6578\u7684\u91cd\u8f09\uff0c\u4f7f\u958b\u767c\u8005\u66f4\u52a0\u65b9\u4fbf\u7684\u958b\u767c\u51fd\u6578\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981:TypeScript\u51fd\u6578\u53ef\u9009\u53c3\u6578\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u52a0\u7c21\u55ae\u5730\u5b9a\u7fa9\u51fd\u6578\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u5f48\u6027\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u4f7f\u7528\u51fd\u6578\u3002\u672c\u6587\u5c07\u4ecb\u7d39TypeScript\u51fd\u6578\u53ef\u9009\u53c3\u6578\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\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-5140","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-1kU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5140","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=5140"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5140\/revisions"}],"predecessor-version":[{"id":5141,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5140\/revisions\/5141"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}