{"id":3808,"date":"2023-07-05T14:27:45","date_gmt":"2023-07-05T06:27:45","guid":{"rendered":"https:\/\/www.lcettsg.cn\/code\/?p=3808"},"modified":"2023-07-05T19:59:29","modified_gmt":"2023-07-05T11:59:29","slug":"ts%e6%95%99%e7%a8%8b%ef%bc%88%e5%b0%9a%e7%a1%85%e8%b0%b7%e6%9d%8e%e8%b6%85%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.lcettsg.cn\/code\/?p=3808","title":{"rendered":"TS\u6559\u7a0b\uff08\u5c1a\u7845\u8c37\u674e\u8d85\uff09"},"content":{"rendered":"\n<p>\u5f00\u53d1\u73af\u5883\u642d\u5efa\uff1a<\/p>\n\n\n\n<p>1.\u5b89\u88c5node<br>2.\u4f7f\u7528npm\u5168\u5c40\u5b89\u88c5typescript: npm i -g typescript<br>3.\u521b\u5efa\u4e00\u4e2ats\u6587\u4ef6<br>4.\u4f7f\u7528tsc\u5bf9ts\u6587\u4ef6\u8fdb\u884c\u7f16\u8bd1\uff1a tsc xxx.ts<\/p>\n\n\n\n<p>\u57fa\u672c\u7c7b\u578b<\/p>\n\n\n\n<p>\u7c7b\u578b\u58f0\u660e<br>\u7c7b\u578b\u58f0\u660e\u662fTS\u975e\u5e38\u91cd\u8981\u7684\u4e00\u4e2a\u7279\u70b9<br>\u901a\u8fc7\u7c7b\u578b\u58f0\u660e\u53ef\u4ee5\u6307\u5b9aTS\u4e2d\u53d8\u91cf\uff08\u53c2\u6570\u3001\u5f62\u53c2)\u7684\u7c7b\u578b<br>\u6307\u5b9a\u7c7b\u578b\u540e\uff0c\u5f53\u4e3a\u53d8\u91cf\u8d4b\u503c\u65f6\uff0cTS\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u68c0\u67e5\u503c\u662f\u5426\u7b26\u5408\u7c7b\u578b\u58f0\u660e\uff0c\u7b26\u5408\u5219\u8d4b\u503c\uff0c\u5426\u5219\u62a5\u9519\u3002<br>\u7b80\u800c\u8a00\u4e4b\uff0c\u7c7b\u578b\u58f0\u660e\u7ed9\u53d8\u91cf\u8bbe\u7f6e\u4e86\u7c7b\u578b\uff0c\u4f7f\u5f97\u53d8\u91cf\u53ea\u80fd\u5b58\u50a8\u67d0\u79cd\u7c7b\u578b\u7684\u503c<br>\u8bed\u6cd5:<br>let\u53d8\u91cf:\u7c7b\u578b;<br>let\u53d8\u91cf:\u7c7b\u578b=\u503c;<br>function fn(\u53c2\u6570:\u7c7b\u578b\uff0c\u53c2\u6570:\u7c7b\u578b):\u7c7b\u578b{<br>&#8230;&#8230;<br>}<br>\u81ea\u52a8\u7c7b\u578b\u5224\u65ad<br>TS\u62e5\u6709\u81ea\u52a8\u7684\u7c7b\u578b\u5224\u65ad\u673a\u5236\u3002<br>\u5f53\u5bf9\u53d8\u91cf\u7684\u58f0\u660e\u548c\u8d4b\u503c\u662f\u540c\u65f6\u8fdb\u884c\u7684\uff0cTS\u7f16\u8bd1\u5668\u4f1a\u81ea\u52a8\u5224\u65ad\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u6240\u4ee5\u5982\u679c\u4f60\u7684\u53d8\u91cf\u7684\u58f0\u660e\u548c\u8d4b\u503c\u65f6\u540c\u65f6\u8fdb\u884c\u7684\uff0c\u53ef\u4ee5\u7701\u7565\u6389\u7c7b\u578b\u58f0\u660e\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1506\" height=\"854\" src=\"https:\/\/www.lcettsg.cn\/code\/wp-content\/uploads\/2023\/07\/image-6.png\" alt=\"\" class=\"wp-image-3811\" srcset=\"https:\/\/www.lcettsg.cn\/code\/wp-content\/uploads\/2023\/07\/image-6.png 1506w, https:\/\/www.lcettsg.cn\/code\/wp-content\/uploads\/2023\/07\/image-6-1000x567.png 1000w, https:\/\/www.lcettsg.cn\/code\/wp-content\/uploads\/2023\/07\/image-6-500x284.png 500w, https:\/\/www.lcettsg.cn\/code\/wp-content\/uploads\/2023\/07\/image-6-768x436.png 768w\" sizes=\"(max-width: 1506px) 100vw, 1506px\" \/><\/figure>\n\n\n\n<p>\u8054\u5408\u7c7b\u578b\uff1a  let b: &#8220;male&#8221; | &#8220;female&#8221;;  let c:boolean |string; c=true; c=&#8217;hello&#8217;; <br>any\u8868\u793a\u7684\u662f\u4efb\u610f\u7c7b\u578b\uff0c\u4e00\u4e2a\u53d8\u91cf\u8bbe\u7f6e\u7c7b\u578b\u4e3aany\u540e\u76f8\u5f53\u4e8e\u5bf9\u8be5\u53d8\u91cf\u5173\u95ed\u4e86TS\u7684\u7c7b\u578b\u68c0\u6d4b\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528\u3002<br>let d: any;(\u663e\u5f0f\u7684any)<br>\u58f0\u660e\u53d8\u91cf\u5982\u679c\u4e0d\u6307\u5b9a\u7c7b\u578b\uff0c\u5219TS\u89e3\u91ca\u5668\u4f1a\u81ea\u52a8\u5224\u65ad\u53d8\u91cf\u7684\u7c7b\u578b\u4e3aany (\u9690\u85cf\u7684any)\u3002<br>\u53d8\u91cfd\u7684\u7c7b\u578b\u4e3aany, \u5b83\u53ef\u4ee5\u8d4b\u503c\u7ed9\u4efb\u610f\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>unknown\u8868\u793a\u672a\u77e5\u7c7b\u578b\u7684\u503c\uff0c\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u7c7b\u578b\u7c7b\u578b\u7684any \u3002unknown\u7c7b\u578b\u7684\u53d8\u91cf\uff0c\u4e0d\u80fd\u76f4\u63a5\u8d4b\u503c\u7ed9\u5176\u4ed6\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>\/\/\u7c7b\u578b\u65ad\u8a00,\u4e24\u79cd\u7528\u6cd5<br>s= e as string;<br>s=&lt;string>e;<\/p>\n\n\n\n<p>never\u8868\u793a\u6c38\u8fdc\u4e0d\u4f1a\u8fd4\u56de\u7ed3\u679c\u3002void\u4e5f\u662f\u6709\u8fd4\u56de\u7ed3\u679c\u3002js\u91cc\u9762\u62a5\u9519\u7684\u51fd\u6570\uff0c\u5982throw new Error(&#8220;\u62a5\u9519\u4e86\u201d)\uff0c\u76f4\u63a5\u7ed3\u675f\uff0c\u4e0d\u7528\u6709\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-small-font-size\"><code>\/\/object\u8868\u793a\u4e00\u4e2ajs\u5bf9\u8c61\nlet a:object;\na={};\na=function(){};\n\n\/\/{}\u7528\u6765\u6307\u5b9a\u5bf9\u8c61\u4e2d\u53ef\u4ee5\u5305\u542b\u54ea\u4e9b\u5c5e\u6027\n\/\/\u8bed\u6cd5\uff1a{\u5c5e\u6027\u540d\uff1a\u5c5e\u6027\u503c\uff0c\u5c5e\u6027\u540d\uff1a\u5c5e\u6027\u503c}\n\/\/\u5728\u5c5e\u6027\u540d\u540e\u8fb9\u52a0\u4e0a\uff1f\uff0c\u8868\u793a\u5c5e\u6027\u503c\u662f\u53ef\u9009\u7684\nlet b:{name:string, age?:number}\nlet c:{name:string,a?:number, b?:number};\nc={name:'\u732a\u516b\u6212'\uff0ca:1,b:2};\n\/\/&#91;propName:string]\uff1aany\u8868\u793a\u4efb\u610f\u7c7b\u578b\u7684\u5c5e\u6027\nlet c:{name:string, &#91;propName:string]:any};\n\n\/\/\u8bbe\u7f6e\u51fd\u6570\u7ed3\u6784\u7684\u7c7b\u578b\u58f0\u660e\uff1a\u8bed\u6cd5\uff1a\uff08\u5f62\u53c2\uff1a\u7c7b\u578b\uff0c\u5f62\u53c2\uff1a\u7c7b\u578b...\uff09=>\u8fd4\u56de\u503c\u7c7b\u578b\nlet d:(a:number,b:number)=>number;\n\n\/\/\u6570\u7ec4\u7684\u7c7b\u578b\u58f0\u660e\uff1a\u7c7b\u578b&#91;],\u6216\u662fArray&lt;\u7c7b\u578b>\n\/\/string&#91;]\u8868\u793a\u5b57\u7b26\u4e32\u6570\u7ec4\nlet e:string&#91;];\n\/\/number&#91;]\u8868\u793a\u6570\u503c\u7c7b\u578b\nlet f:number&#91;];\nlet g:Array&lt;number>\n\n\/\/\u5143\u7ec4\uff0c\u5143\u7ec4\u5c31\u662f\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\u3002\n\/\/\u8bed\u6cd5\uff1a&#91;\u7c7b\u578b\uff0c\u7c7b\u578b\uff0c\u7c7b\u578b]\nlet h:&#91;string,string];\nh=&#91;'hello','abc'];\n\n\/\/enum \u679a\u4e3e\nlet i:{name:string,gender:string};\ni={name:'\u5b59\u609f\u7a7a',gender:'\u7537'};\n\nenum Gender{\n  Male;\n  Female;\n}\ni={name:'\u5b59\u609f\u7a7a',gender:Gender.Male};\n\n\/\/&amp;\u8868\u793a\u540c\u65f6\nlet j:{name:string} &amp; {age:number};\nj={name:'\u5b59\u609f\u7a7a'\uff0cage:18};\n\n\/\/\u7c7b\u578b\u7684\u522b\u540d\ntype myType=string;\nlet m:myType.\n\ntype myType = 1|2|3|4|5;\nlet k:myType;\nlet l:myTpye;\n<\/code><\/pre>\n\n\n\n<p>\u7f16\u8bd1\u9009\u9879\uff1a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f00\u53d1\u73af\u5883\u642d\u5efa\uff1a 1.\u5b89\u88c5node2.\u4f7f\u7528npm\u5168\u5c40\u5b89\u88c5typescript: npm i -g typescr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-3808","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.lcettsg.cn\/code\/index.php?rest_route=\/wp\/v2\/posts\/3808"}],"collection":[{"href":"https:\/\/www.lcettsg.cn\/code\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lcettsg.cn\/code\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lcettsg.cn\/code\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lcettsg.cn\/code\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3808"}],"version-history":[{"count":14,"href":"https:\/\/www.lcettsg.cn\/code\/index.php?rest_route=\/wp\/v2\/posts\/3808\/revisions"}],"predecessor-version":[{"id":3835,"href":"https:\/\/www.lcettsg.cn\/code\/index.php?rest_route=\/wp\/v2\/posts\/3808\/revisions\/3835"}],"wp:attachment":[{"href":"https:\/\/www.lcettsg.cn\/code\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lcettsg.cn\/code\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lcettsg.cn\/code\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}