{"id":3121,"date":"2024-11-10T15:20:12","date_gmt":"2024-11-10T07:20:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3121\/"},"modified":"2024-11-10T15:20:12","modified_gmt":"2024-11-10T07:20:12","slug":"typescript%e5%85%a5%e9%97%a8%e5%88%b0%e7%b2%be%e9%80%9a%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3121\/","title":{"rendered":"typescript\u5165\u95e8\u5230\u7cbe\u901a\u6559\u7a0b"},"content":{"rendered":"<blockquote><p>\n  \u8981\u5f00\u59cb\u4f7f\u7528 typescript\uff0c\u60a8\u9700\u8981\u5b89\u88c5 node.js \u548c typescript \u7f16\u8bd1\u5668\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a typescript \u6587\u4ef6\u3002typescript \u5f15\u5165\u4e86\u7c7b\u578b\u7cfb\u7edf\uff0c\u5305\u62ec\u539f\u59cb\u7c7b\u578b\u3001\u5bf9\u8c61\u7c7b\u578b\u3001\u6570\u7ec4\u7c7b\u578b\u3001\u679a\u4e3e\u7c7b\u578b\u3001\u63a5\u53e3\u548c\u7c7b\u3002\u5b83\u8fd8\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u5141\u8bb8\u60a8\u521b\u5efa\u7c7b\u3001\u5bf9\u8c61\u3001\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u8981\u7f16\u8bd1 typescript \u4ee3\u7801\uff0c\u8bf7\u4f7f\u7528\u547d\u4ee4 tsc my-app.ts\uff0c\u7136\u540e\u60a8\u53ef\u4ee5\u8fd0\u884c\u751f\u6210\u7684 javascript \u6587\u4ef6\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110803064070399.jpg\" class=\"aligncenter\" title=\"typescript\u5165\u95e8\u5230\u7cbe\u901a\u6559\u7a0b\u63d2\u56fe\" alt=\"typescript\u5165\u95e8\u5230\u7cbe\u901a\u6559\u7a0b\u63d2\u56fe\" \/><\/p>\n<p><strong>TypeScript \u5165\u95e8\u5230\u7cbe\u901a<\/strong><\/p>\n<p><strong>\u5165\u95e8<\/strong><\/p>\n<p>TypeScript \u662f\u4e00\u79cd\u6d41\u884c\u7684 JavaScript \u8d85\u96c6\uff0c\u5b83\u4e3a JavaScript \u6dfb\u52a0\u4e86\u7c7b\u578b\u7cfb\u7edf\u3002\u8fd9\u4f7f\u5f97\u5b83\u66f4\u5bb9\u6613\u7f16\u5199\u548c\u7ef4\u62a4\u5927\u578b JavaScript \u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u8981\u5f00\u59cb\u4f7f\u7528 TypeScript\uff0c\u60a8\u9700\u8981\uff1a<\/p>\n<ul>\n<li>\u5b89\u88c5 Node.js<\/li>\n<li>\u5b89\u88c5 TypeScript \u7f16\u8bd1\u5668\uff1anpm install -g  <\/li>\n<li>\u521b\u5efa\u4e00\u4e2a TypeScript \u6587\u4ef6\uff1amy-app.ts<\/li>\n<\/ul>\n<p><strong>\u57fa\u672c\u8bed\u6cd5<\/strong><\/p>\n<p>TypeScript \u7684\u8bed\u6cd5\u4e0e JavaScript \u975e\u5e38\u76f8\u4f3c\uff0c\u4f46\u5b83\u589e\u52a0\u4e86\u7c7b\u578b\u6ce8\u91ca\u3002\u8fd9\u4e9b\u6ce8\u91ca\u544a\u8bc9\u7f16\u8bd1\u5668\u53d8\u91cf\u3001\u51fd\u6570\u548c\u7c7b\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>let myNumber: number = 10;\nfunction add(a: number, b: number): number {\n  return a + b;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7c7b\u578b\u7cfb\u7edf<\/strong><\/p>\n<p>TypeScript \u7684\u7c7b\u578b\u7cfb\u7edf\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> <strong>\u539f\u59cb\u7c7b\u578b\uff1a<\/strong> number\u3001string\u3001boolean\u3001null \u548c undefined<\/li>\n<li> <strong>\u5bf9\u8c61\u7c7b\u578b\uff1a<\/strong> any\u3001unknown\u3001void \u548c never<\/li>\n<li> <strong>\u6570\u7ec4\u7c7b\u578b\uff1a<\/strong> number[]\u3001string[]\u3001any[]<\/li>\n<li> <strong>\u679a\u4e3e\u7c7b\u578b\uff1a<\/strong> enum<\/li>\n<li> <strong>\u63a5\u53e3\uff1a<\/strong> \u5b9a\u4e49\u4e86\u5bf9\u8c61\u7684\u5f62\u72b6<\/li>\n<li> <strong>\u7c7b\uff1a<\/strong> \u5141\u8bb8\u521b\u5efa\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b<\/li>\n<\/ul>\n<p><strong>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b<\/strong><\/p>\n<p>TypeScript \u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 TypeScript \u521b\u5efa\u7c7b\u3001\u5bf9\u8c61\u3001\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>class Person {\n  name: string;\n  age: number;\n\n  constructor(name: string, age: number) {\n    this.name = name;\n    this.age = age;\n  }\n\n  getGreeting(): string {\n    return `Hello, my name is ${this.name} and I am ${this.age} years old.`;\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7f16\u8bd1\u548c\u8fd0\u884c<\/strong><\/p>\n<p>\u8981\u7f16\u8bd1 TypeScript \u4ee3\u7801\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>tsc my-app.ts<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u751f\u6210\u7684 JavaScript \u4ee3\u7801\u5c06\u4fdd\u5b58\u5728 my-app.js \u4e2d\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Node.js \u6216\u6d4f\u89c8\u5668\u8fd0\u884c\u8be5\u6587\u4ef6\u3002<\/p>\n<p><strong>\u8fdb\u9636<\/strong><\/p>\n<p>\u6df1\u5165\u5b66\u4e60 TypeScript \u540e\uff0c\u60a8\u53ef\u4ee5\u63a2\u7d22\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li> <strong>\u6cdb\u578b\uff1a<\/strong> \u521b\u5efa\u53ef\u4e0e\u4efb\u4f55\u7c7b\u578b\u4e00\u8d77\u4f7f\u7528\u7684\u4ee3\u7801<\/li>\n<li> <strong>\u88c5\u9970\u5668\uff1a<\/strong> \u4fee\u6539\u7c7b\u7684\u884c\u4e3a<\/li>\n<li> <strong>\u5f02\u6b65\u7f16\u7a0b\uff1a<\/strong> \u5904\u7406\u5f02\u6b65\u64cd\u4f5c<\/li>\n<\/ul>\n<p><strong>\u8d44\u6e90<\/strong><\/p>\n<ul>\n<li>[TypeScript \u5b98\u65b9\u6587\u6863](https:\/\/www.typescriptlang.org\/docs\/)<\/li>\n<li>[TypeScript \u5165\u95e8\u6559\u7a0b](https:\/\/www.tutoriaoint.com\/typescript\/typescript_tutorial.htm)<\/li>\n<li>[TypeScript \u793e\u533a\u8bba\u575b](https:\/\/hub.com\/microsoft\/TypeScript\/discussions)<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5165\u95e8\u5230\u7cbe\u901a\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>\u8981\u5f00\u59cb\u4f7f\u7528 typescript\uff0c\u60a8\u9700\u8981\u5b89\u88c5 node.js \u548c typescript \u7f16\u8bd1\u5668\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a typescript \u6587\u4ef6\u3002typescript \u5f15\u5165\u4e86\u7c7b\u578b\u7cfb\u7edf\uff0c\u5305\u62ec\u539f\u59cb\u7c7b\u578b\u3001\u5bf9\u8c61\u7c7b\u578b\u3001\u6570\u7ec4\u7c7b\u578b\u3001\u679a\u4e3e\u7c7b\u578b\u3001\u63a5\u53e3\u548c\u7c7b\u3002\u5b83\u8fd8\u652f\u6301\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u5141\u8bb8\u60a8\u521b\u5efa\u7c7b\u3001\u5bf9\u8c61\u3001\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u8981\u7f16\u8bd1 typescript \u4ee3\u7801\uff0c\u8bf7\u4f7f\u7528\u547d\u4ee4 tsc my-app.ts\uff0c\u7136\u540e\u60a8\u53ef\u4ee5\u8fd0\u884c\u751f\u6210\u7684 javascript \u6587\u4ef6\u3002 TypeScript \u5165\u95e8\u5230\u7cbe\u901a \u5165\u95e8 TypeScript \u662f\u4e00\u79cd\u6d41\u884c\u7684 JavaScript \u8d85\u96c6\uff0c\u5b83\u4e3a JavaScript \u6dfb\u52a0\u4e86\u7c7b\u578b\u7cfb\u7edf\u3002\u8fd9\u4f7f\u5f97\u5b83\u66f4\u5bb9\u6613\u7f16\u5199\u548c\u7ef4\u62a4\u5927\u578b JavaScript \u5e94\u7528\u7a0b\u5e8f\u3002 \u8981\u5f00\u59cb\u4f7f\u7528 TypeScript\uff0c\u60a8\u9700\u8981\uff1a \u5b89\u88c5 Node.js \u5b89\u88c5 TypeScript \u7f16\u8bd1\u5668\uff1anpm install -g \u521b\u5efa\u4e00\u4e2a TypeScript \u6587\u4ef6\uff1amy-app.ts \u57fa\u672c\u8bed\u6cd5 TypeScript \u7684\u8bed\u6cd5\u4e0e JavaScript \u975e\u5e38\u76f8\u4f3c\uff0c\u4f46\u5b83\u589e\u52a0\u4e86\u7c7b\u578b\u6ce8\u91ca\u3002\u8fd9\u4e9b\u6ce8\u91ca\u544a\u8bc9\u7f16\u8bd1\u5668\u53d8\u91cf\u3001\u51fd\u6570\u548c\u7c7b\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a let myNumber: number = 10; function add(a: number, b: number): number { [&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-3121","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3121","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=3121"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3121\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}