{"id":5402,"date":"2025-05-28T13:22:34","date_gmt":"2025-06-04T06:15:33","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5402"},"modified":"2025-06-04T13:22:34","modified_gmt":"2025-06-04T06:15:33","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e6%b1%82%e5%8f%96%e6%95%b8%e7%b5%84%e7%9a%84%e7%b7%9a%e6%80%a7%e5%9b%9e%e6%ad%b8%e4%bf%82%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8typescript%e6%b1%82%e5%8f%96%e6%95%b8%e7%b5%84%e7%9a%84%e7%b7%9a%e6%80%a7%e5%9b%9e%e6%ad%b8%e4%bf%82%e6%95%b8\/","title":{"rendered":"\u4f7f\u7528 TypeScript \u8f15\u9b06\u6c42\u53d6\u6578\u7d44\u7684\u7dda\u6027\u56de\u6b78\u4fc2\u6578\uff1a\u5b8c\u6574\u6559\u5b78\u8207\u5be6\u4f5c\u7bc4\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u6578\u7d44, \u7dda\u6027\u56de\u6b78, \u4fc2\u6578, \u6578\u64da\u5206\u6790\"><\/p>\n<h1>\u4f7f\u7528 TypeScript \u8f15\u9b06\u6c42\u53d6\u6578\u7d44\u7684\u7dda\u6027\u56de\u6b78\u4fc2\u6578<\/h1>\n<p>\u7dda\u6027\u56de\u6b78\u662f\u4e00\u7a2e\u5f37\u5927\u7684\u6578\u64da\u5206\u6790\u6280\u8853\uff0c\u7528\u65bc\u63a2\u7d22\u548c\u9810\u6e2c\u8b8a\u91cf\u4e4b\u9593\u7684\u95dc\u4fc2\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 TypeScript \u4f86\u6c42\u53d6\u6578\u7d44\u7684\u7dda\u6027\u56de\u6b78\u4fc2\u6578\uff0c\u5305\u62ec\u659c\u7387\u548c\u622a\u8ddd\u7684\u8a08\u7b97\u904e\u7a0b\u3002<\/p>\n<h2>\u7dda\u6027\u56de\u6b78\u7c21\u4ecb<\/h2>\n<p>\u7dda\u6027\u56de\u6b78\u662f\u4e00\u7a2e\u7d71\u8a08\u5206\u6790\u65b9\u6cd5\uff0c\u65e8\u5728\u5efa\u7acb\u4e00\u500b\u8b8a\u91cf\uff08\u56e0\u8b8a\u91cf\uff09\u8207\u53e6\u4e00\u500b\u8b8a\u91cf\uff08\u81ea\u8b8a\u91cf\uff09\u4e4b\u9593\u7684\u95dc\u4fc2\u6a21\u578b\u3002\u5b83\u7684\u57fa\u672c\u5f62\u5f0f\u662f\u4e00\u500b\u7dda\u6027\u65b9\u7a0b\uff1a<\/p>\n<pre class=\"brush: python\">\ny = mx + b\n<\/pre>\n<p>\u5728\u9019\u88e1\uff1a<br \/>\n&#8211; <strong>y<\/strong> \u662f\u6211\u5011\u8981\u9810\u6e2c\u7684\u8b8a\u91cf\u3002<br \/>\n&#8211; <strong>x<\/strong> \u662f\u81ea\u8b8a\u91cf\u3002<br \/>\n&#8211; <strong>m<\/strong> \u662f\u659c\u7387\uff0c\u8868\u793a x \u7684\u8b8a\u5316\u5c0d y \u7684\u5f71\u97ff\u7a0b\u5ea6\u3002<br \/>\n&#8211; <strong>b<\/strong> \u662f\u622a\u8ddd\uff0c\u8868\u793a\u7576 x \u70ba\u96f6\u6642 y \u7684\u9810\u6e2c\u503c\u3002<\/p>\n<p>\u8981\u8a08\u7b97\u7dda\u6027\u56de\u6b78\u4fc2\u6578\uff0c\u6211\u5011\u9700\u8981\u6c42\u53d6\u659c\u7387 <strong>m<\/strong> \u548c\u622a\u8ddd <strong>b<\/strong>\uff0c\u9019\u6d89\u53ca\u5230\u8a08\u7b97\u5e73\u5747\u503c\u548c\u5171\u8b8a\u7570\u6578\u3002<\/p>\n<h2>\u5728 TypeScript \u4e2d\u5be6\u73fe\u7dda\u6027\u56de\u6b78<\/h2>\n<p>\u5728 TypeScript \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u5229\u7528 `Array.prototype.reduce()` \u65b9\u6cd5\u4f86\u5e6b\u52a9\u6211\u5011\u8a08\u7b97\u6240\u9700\u7684\u7d71\u8a08\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u5be6\u73fe\u7684\u6b65\u9a5f\uff1a<\/p>\n<p>1. \u8a08\u7b97\u6578\u7d44\u7684\u5e73\u5747\u503c\u3002<br \/>\n2. \u8a08\u7b97 x \u548c y \u7684\u5171\u8b8a\u7570\u6578\u3002<br \/>\n3. \u8a08\u7b97 x \u7684\u65b9\u5dee\u3002<br \/>\n4. \u4f7f\u7528\u516c\u5f0f\u8a08\u7b97\u659c\u7387 <strong>m<\/strong> \u548c\u622a\u8ddd <strong>b<\/strong>\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684 TypeScript \u4ee3\u78bc\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: python\">\nfunction linearRegressionCoefficients(x: number[], y: number[]): [number, number] {\n    const n = x.length;\n    const xMean = x.reduce((acc, curr) => acc + curr, 0) \/ n;\n    const yMean = y.reduce((acc, curr) => acc + curr, 0) \/ n;\n\n    const xyCovariance = x.reduce((acc, curr, i) => acc + (curr - xMean) * (y[i] - yMean), 0);\n    const xVariance = x.reduce((acc, curr) => acc + (curr - xMean) ** 2, 0);\n\n    const m = xyCovariance \/ xVariance;\n    const b = yMean - m * xMean;\n\n    return [m, b];\n}\n<\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u9996\u5148\u8a08\u7b97 x \u548c y \u7684\u5e73\u5747\u503c\uff0c\u7136\u5f8c\u8a08\u7b97\u5b83\u5011\u7684\u5171\u8b8a\u7570\u6578\u548c x \u7684\u65b9\u5dee\uff0c\u6700\u5f8c\u6839\u64da\u516c\u5f0f\u8a08\u7b97\u51fa\u659c\u7387\u548c\u622a\u8ddd\u4e26\u8fd4\u56de\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u4f7f\u7528 TypeScript \u4f86\u6c42\u53d6\u6578\u7d44\u7684\u7dda\u6027\u56de\u6b78\u4fc2\u6578\u662f\u4e00\u9805\u5be6\u7528\u7684\u6280\u8853\uff0c\u80fd\u5e6b\u52a9\u6211\u5011\u6df1\u5165\u7406\u89e3\u6578\u64da\u9593\u7684\u95dc\u4fc2\u4e26\u9810\u6e2c\u672a\u4f86\u8da8\u52e2\u3002\u7121\u8ad6\u4f60\u662f\u6578\u64da\u5206\u6790\u5e2b\u9084\u662f\u958b\u767c\u8005\uff0c\u638c\u63e1\u9019\u9805\u6280\u80fd\u90fd\u5c07\u5c0d\u4f60\u6709\u6240\u5e6b\u52a9\u3002\u958b\u59cb\u5be6\u4f5c\u4e26\u5206\u6790\u4f60\u7684\u6578\u64da\u5427\uff01<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528TypeScript\u6c42\u53d6\u6578\u7d44\u7684\u7dda\u6027\u56de\u6b78\u4fc2\u6578\uff0c\u8a73\u7d30\u89e3\u91cb\u4e86\u7dda\u6027\u56de\u6b78\u7684\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528TypeScript\u7684\u65b9\u6cd5\u4f86\u6c42\u53d6\u6578\u7d44\u7684\u7dda\u6027\u56de\u6b78\u4fc2\u6578\uff0c\u8b93\u8b80\u8005\u80fd\u5920\u66f4\u52a0\u4e86\u89e3TypeScript\u7684\u529f\u80fd\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-5402","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-1p8","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5402","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=5402"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5402\/revisions"}],"predecessor-version":[{"id":5403,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5402\/revisions\/5403"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}