{"id":3120,"date":"2024-11-10T08:35:25","date_gmt":"2024-11-10T00:35:25","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3120\/"},"modified":"2024-11-10T08:35:25","modified_gmt":"2024-11-10T00:35:25","slug":"typescript%e8%87%aa%e5%ad%a6%e5%85%a5%e9%97%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3120\/","title":{"rendered":"typescript\u81ea\u5b66\u5165\u95e8\u6559\u7a0b"},"content":{"rendered":"<blockquote><p>\n  typescript\u662f\u4e00\u79cd\u8d85\u8d8ajavascript\u7684\u9759\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u589e\u52a0\u4e86\u7c7b\u578b\u68c0\u67e5\u529f\u80fd\u3002\u5176\u4f18\u52bf\u5305\u62ec\uff1a\u66f4\u5f3a\u7684\u4ee3\u7801\u5b89\u5168\u6027\uff1a\u7c7b\u578b\u68c0\u67e5\u68c0\u6d4b\u9519\u8bef\uff0c\u9632\u6b62\u6076\u610f\u8f93\u5165\u3002\u66f4\u9ad8\u7684\u53ef\u7ef4\u62a4\u6027\uff1a\u660e\u786e\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\uff0c\u63d0\u9ad8\u4ee3\u7801\u6613\u8bfb\u6027\u3002\u66f4\u4f73\u5f00\u53d1\u4f53\u9a8c\uff1a\u63d0\u4f9b\u81ea\u52a8\u5b8c\u6210\u529f\u80fd\u3001\u9519\u8bef\u63d0\u793a\u548c\u91cd\u6784\u5de5\u5177\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110803042183851.jpg\" class=\"aligncenter\" title=\"typescript\u81ea\u5b66\u5165\u95e8\u6559\u7a0b\u63d2\u56fe\" alt=\"typescript\u81ea\u5b66\u5165\u95e8\u6559\u7a0b\u63d2\u56fe\" \/><\/p>\n<p><strong>TypeScript \u81ea\u5b66\u5165\u95e8\u6559\u7a0b<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>TypeScript \u662f\u4e00\u79cd\u8d85\u8d8a JavaScript \u7684\u5f00\u6e90\u7f16\u7a0b\u8bed\u8a00\uff0c\u6269\u5c55\u4e86\u5176\u529f\u80fd\u5e76\u589e\u52a0\u4e86\u7c7b\u578b\u68c0\u67e5\u7279\u6027\u3002\u5b83\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u7f16\u5199\u51fa\u66f4\u5065\u58ee\u3001\u66f4\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>\u4ec0\u4e48\u662f TypeScript\uff1f<\/strong><\/p>\n<p>TypeScript \u662f\u4e00\u79cd\u9759\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u5b83\u5728\u7f16\u8bd1\u65f6\u68c0\u67e5\u4ee3\u7801\u4e2d\u7c7b\u578b\u9519\u8bef\uff0c\u4f7f\u5176\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u4e4b\u524d\u53d1\u73b0\u6f5c\u5728\u9519\u8bef\u3002\u5b83\u7f16\u8bd1\u4e3a\u7eaf JavaScript \u4ee3\u7801\uff0c\u5728\u6240\u6709\u652f\u6301 JavaScript \u7684\u5e73\u53f0\u4e0a\u8fd0\u884c\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u8981\u5b66\u4e60 TypeScript\uff1f<\/strong><\/p>\n<ul>\n<li> <strong>\u66f4\u5f3a\u7684\u4ee3\u7801\u5b89\u5168\u6027\uff1a<\/strong>\u7c7b\u578b\u68c0\u67e5\u6709\u52a9\u4e8e\u68c0\u6d4b\u9519\u8bef\uff0c\u9632\u6b62\u6076\u610f\u8f93\u5165\u5e76\u786e\u4fdd\u4ee3\u7801\u7684\u4e00\u81f4\u6027\u3002<\/li>\n<li> <strong>\u66f4\u9ad8\u7684\u53ef\u7ef4\u62a4\u6027\uff1a<\/strong>\u901a\u8fc7\u660e\u786e\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\uff0cTypeScript \u4ee3\u7801\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<li> <strong>\u66f4\u597d\u7684\u5f00\u53d1\u4eba\u5458\u4f53\u9a8c\uff1a<\/strong>TypeScript \u63d0\u4f9b\u4ee3\u7801\u81ea\u52a8\u5b8c\u6210\u529f\u80fd\u3001\u9519\u8bef\u63d0\u793a\u548c\u91cd\u6784\u5de5\u5177\uff0c\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/li>\n<\/ul>\n<p><strong>\u5165\u95e8\u6b65\u9aa4<\/strong><\/p>\n<p><strong>1. \u5b89\u88c5 TypeScript<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 npm\uff1anpm i -g  <\/li>\n<li>\u4f7f\u7528 yarn\uff1ayarn global add typescript<\/li>\n<\/ul>\n<p><strong>2. \u521b\u5efa TypeScript \u9879\u76ee<\/strong><\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\uff1amkdir my-typescript-project<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a tsconfig.json \u6587\u4ef6\u6765\u914d\u7f6e TypeScript \u7f16\u8bd1\u5668\uff1a<\/li>\n<\/ul>\n<pre>{\n  \"compilerOptions\": {\n    \"target\": \"es2015\",\n    \"module\": \"commonjs\"\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a TypeScript \u6587\u4ef6\uff0c\u4f8b\u5982 mn.ts\uff1a<\/li>\n<\/ul>\n<pre>let name: string = \"John\";\nconsole.log(`Hello, ${name}!`);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u7f16\u8bd1 TypeScript \u4ee3\u7801<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 tsc \u547d\u4ee4\u7f16\u8bd1 TypeScript \u6587\u4ef6\uff1atsc main.ts<\/li>\n<li>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a main.js \u6587\u4ef6\uff0c\u5305\u542b\u7f16\u8bd1\u540e\u7684 JavaScript \u4ee3\u7801\u3002<\/li>\n<\/ul>\n<p><strong>4. \u8fd0\u884c JavaScript \u4ee3\u7801<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 Node.js \u8fd0\u884c\u751f\u6210\u7684 JavaScript \u4ee3\u7801\uff1anode main.js<\/li>\n<\/ul>\n<p><strong>\u8fdb\u9636\u6b65\u9aa4<\/strong><\/p>\n<p><strong>\u7c7b\u578b\u7cfb\u7edf<\/strong><\/p>\n<ul>\n<li>TypeScript \u63d0\u4f9b\u4e86\u5e7f\u6cdb\u7684\u5185\u7f6e\u7c7b\u578b\uff0c\u4f8b\u5982 string\u3001number \u548c boolean\u3002<\/li>\n<li>\u81ea\u5b9a\u4e49\u7c7b\u578b\u53ef\u7528\u4e8e\u5b9a\u4e49\u590d\u6742\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<li>\u679a\u4e3e\u53ef\u7528\u4e8e\u9650\u5236\u53d8\u91cf\u7684\u503c\u8303\u56f4\u3002<\/li>\n<\/ul>\n<p><strong>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b<\/strong><\/p>\n<ul>\n<li>TypeScript \u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3002<\/li>\n<li>\u7c7b\u548c\u63a5\u53e3\u53ef\u7528\u4e8e\u5b9a\u4e49\u548c\u7ba1\u7406\u5bf9\u8c61\u3002<\/li>\n<li>\u7ee7\u627f\u548c\u591a\u6001\u6027\u5141\u8bb8\u4ee3\u7801\u91cd\u7528\u548c\u53ef\u6269\u5c55\u6027\u3002<\/li>\n<\/ul>\n<p><strong>\u51fd\u6570\u548c\u6cdb\u578b<\/strong><\/p>\n<ul>\n<li>TypeScript \u652f\u6301\u7c7b\u578b\u5316\u7684\u51fd\u6570\u548c\u6cdb\u578b\u3002<\/li>\n<li>\u6cdb\u578b\u5141\u8bb8\u521b\u5efa\u53ef\u4e0e\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u4e00\u8d77\u5de5\u4f5c\u7684\u4ee3\u7801\u3002<\/li>\n<li>\u7c7b\u578b\u63a8\u65ad\u548c\u81ea\u52a8\u7c7b\u578b\u6ce8\u91ca\u6709\u52a9\u4e8e\u7b80\u5316\u4ee3\u7801\u5f00\u53d1\u3002<\/li>\n<\/ul>\n<p><strong>\u5176\u4ed6\u8d44\u6e90<\/strong><\/p>\n<ul>\n<li>[TypeScript \u5b98\u65b9\u6587\u6863](https:\/\/www.typescriptlang.org\/docs\/handbook\/typescript-in-5-minutes.html)<\/li>\n<li>[Tyler McGinnis \u7684 TypeScript \u6559\u7a0b](https:\/\/tylermcginnis.com\/react-router-tutorial\/)<\/li>\n<li>[Codecademy TypeScript \u8bfe\u7a0b](https:\/\/www.codecademy.com\/learn\/typescript)<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u81ea\u5b66\u5165\u95e8\u6559\u7a0b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>typescript\u662f\u4e00\u79cd\u8d85\u8d8ajavascript\u7684\u9759\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u589e\u52a0\u4e86\u7c7b\u578b\u68c0\u67e5\u529f\u80fd\u3002\u5176\u4f18\u52bf\u5305\u62ec\uff1a\u66f4\u5f3a\u7684\u4ee3\u7801\u5b89\u5168\u6027\uff1a\u7c7b\u578b\u68c0\u67e5\u68c0\u6d4b\u9519\u8bef\uff0c\u9632\u6b62\u6076\u610f\u8f93\u5165\u3002\u66f4\u9ad8\u7684\u53ef\u7ef4\u62a4\u6027\uff1a\u660e\u786e\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\uff0c\u63d0\u9ad8\u4ee3\u7801\u6613\u8bfb\u6027\u3002\u66f4\u4f73\u5f00\u53d1\u4f53\u9a8c\uff1a\u63d0\u4f9b\u81ea\u52a8\u5b8c\u6210\u529f\u80fd\u3001\u9519\u8bef\u63d0\u793a\u548c\u91cd\u6784\u5de5\u5177\u3002 TypeScript \u81ea\u5b66\u5165\u95e8\u6559\u7a0b \u7b80\u4ecb TypeScript \u662f\u4e00\u79cd\u8d85\u8d8a JavaScript \u7684\u5f00\u6e90\u7f16\u7a0b\u8bed\u8a00\uff0c\u6269\u5c55\u4e86\u5176\u529f\u80fd\u5e76\u589e\u52a0\u4e86\u7c7b\u578b\u68c0\u67e5\u7279\u6027\u3002\u5b83\u53ef\u4ee5\u8ba9\u5f00\u53d1\u8005\u7f16\u5199\u51fa\u66f4\u5065\u58ee\u3001\u66f4\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\u3002 \u4ec0\u4e48\u662f TypeScript\uff1f TypeScript \u662f\u4e00\u79cd\u9759\u6001\u7c7b\u578b\u8bed\u8a00\uff0c\u5b83\u5728\u7f16\u8bd1\u65f6\u68c0\u67e5\u4ee3\u7801\u4e2d\u7c7b\u578b\u9519\u8bef\uff0c\u4f7f\u5176\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u4e4b\u524d\u53d1\u73b0\u6f5c\u5728\u9519\u8bef\u3002\u5b83\u7f16\u8bd1\u4e3a\u7eaf JavaScript \u4ee3\u7801\uff0c\u5728\u6240\u6709\u652f\u6301 JavaScript \u7684\u5e73\u53f0\u4e0a\u8fd0\u884c\u3002 \u4e3a\u4ec0\u4e48\u8981\u5b66\u4e60 TypeScript\uff1f \u66f4\u5f3a\u7684\u4ee3\u7801\u5b89\u5168\u6027\uff1a\u7c7b\u578b\u68c0\u67e5\u6709\u52a9\u4e8e\u68c0\u6d4b\u9519\u8bef\uff0c\u9632\u6b62\u6076\u610f\u8f93\u5165\u5e76\u786e\u4fdd\u4ee3\u7801\u7684\u4e00\u81f4\u6027\u3002 \u66f4\u9ad8\u7684\u53ef\u7ef4\u62a4\u6027\uff1a\u901a\u8fc7\u660e\u786e\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\uff0cTypeScript \u4ee3\u7801\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002 \u66f4\u597d\u7684\u5f00\u53d1\u4eba\u5458\u4f53\u9a8c\uff1aTypeScript \u63d0\u4f9b\u4ee3\u7801\u81ea\u52a8\u5b8c\u6210\u529f\u80fd\u3001\u9519\u8bef\u63d0\u793a\u548c\u91cd\u6784\u5de5\u5177\uff0c\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002 \u5165\u95e8\u6b65\u9aa4 1. \u5b89\u88c5 TypeScript \u4f7f\u7528 npm\uff1anpm i -g \u4f7f\u7528 yarn\uff1ayarn global add typescript 2. \u521b\u5efa TypeScript \u9879\u76ee \u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\uff1amkdir my-typescript-project \u521b\u5efa\u4e00\u4e2a tsconfig.json \u6587\u4ef6\u6765\u914d\u7f6e TypeScript \u7f16\u8bd1\u5668\uff1a { &#8220;compilerOptions&#8221;: { &#8220;target&#8221;: &#8220;es2015&#8221;, &#8220;module&#8221;: &#8220;commonjs&#8221; } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3120","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3120"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3120\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}