TypeScript基礎之基本類型檢查

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"📌 爲什麼要使用TypeScript?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"JavaScript","attrs":{}}],"attrs":{}},{"type":"text","text":"設計之初只是爲了補充","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Java","attrs":{}}],"attrs":{}},{"type":"text","text":"的,在瀏覽器上做一些小的效果,並不是爲了做大型複雜項目而開發的,js本身也是有很多缺陷的,關於爲什麼要用TS,小夥伴們可以瞅瞅這篇文章 :","attrs":{}},{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/40ce1a483b844199a04e08f00","title":"","type":null},"content":[{"type":"text","text":"https://xie.infoq.cn/article/40ce1a483b844199a04e08f00","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"😛 基本類型檢查","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1. 如何進行類型約束","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"類型約束其實很簡單,只需要在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"變量","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"函數參數","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"函數返回值","attrs":{}}],"attrs":{}},{"type":"text","text":"位置上加上","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":":類型","attrs":{}}],"attrs":{}},{"type":"text","text":"就可以了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"舉個栗子🌰:","attrs":{}},{"type":"text","text":" 變量","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"//我們定義變量的時候,肯定是知道這個變量是存放什麼類型的數據\n\nlet name:string = \"法醫\";\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一旦給 name 賦值其它類型,立馬會提示錯誤","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d3/d30c04c67952b173d5fe14f86e16bb01.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"舉個栗子🌰:","attrs":{}},{"type":"text","text":" 函數參數和返回值","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"//參數後面 :number表示傳的參數必須是數字類型,而test函數後面的 :number表示返回值是數字類型\nfunction test(a:number,b:number):number {\n return a + b;\n}\ntest(1,2);//當調用test函數傳值爲數字表示可以正常運行,傳其它類型則會報錯\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"傳入字符串就會報錯","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/56/56619a2c0d5e8d05979395a968de0310.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當我們寫一個函數的時候,我們非常清楚函數的參數以及返回值是什麼類型的,此時我們可以約束好類型,在之後的調用中我們可以放心的調用函數,因爲只要寫錯了,立馬會提示錯誤,不需要等到程序運行後再提示錯誤,這些在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"JS","attrs":{}}],"attrs":{}},{"type":"text","text":"中是做不到的,但是在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TS","attrs":{}}],"attrs":{}},{"type":"text","text":"中很輕鬆可以做到,不僅如此,類型檢查還帶來很多好處,比方說:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"舉個栗子🌰:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"JS","attrs":{}}],"attrs":{}},{"type":"text","text":"中我們是沒有辦法確定下面代碼中","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"text(1,2)","attrs":{}}],"attrs":{}},{"type":"text","text":"調用的就是一個函數,中途有可能","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"test","attrs":{}}],"attrs":{}},{"type":"text","text":"會被修改,然後調用函數就會報錯","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"function test(a,b) {\n return a + b;\n}\n// 很多行代碼\ntest = 123;\n// 很多行代碼\ntest(1,2);\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/70/701667957e572b8b391a66302288740e.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TS","attrs":{}}],"attrs":{}},{"type":"text","text":"中這種情況絕對是不允許的,“呦呦,切克鬧,我是TS,不允許鴨不允許”🙈","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/69/693b6b133c368dac44cb21e472c13fde.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由於","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TS","attrs":{}}],"attrs":{}},{"type":"text","text":"知道函數test和調用函數test是同一個東西,於是就出現一個神奇的效果——當需要給函數重新命名的時候,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"雙擊函數test","attrs":{}}],"attrs":{}},{"type":"text","text":"並且按","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"F2","attrs":{}}],"attrs":{}},{"type":"text","text":",函數名改了,調用函數名也跟着改了,之所以會達到這個效果,是因爲","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TS","attrs":{}}],"attrs":{}},{"type":"text","text":"有嚴格的類型檢查系統,它知道調用函數的test用的就是test函數,它們兩者之間是建立聯繫的","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/7b/7b2ac16def36179b16ba8648f1161e5d.gif","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不僅如此,還有一種效果:當我們","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"點擊調用函數","attrs":{}}],"attrs":{}},{"type":"text","text":"並且按","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"F12","attrs":{}}],"attrs":{}},{"type":"text","text":",它會跳到定義的函數位置,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2d/2d2c146ed0f41fea335bbd1fb26f30fa.gif","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了讓我們少寫點代碼,使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TS","attrs":{}}],"attrs":{}},{"type":"text","text":"進行約束的時候,TS在很多場景中可以完成","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"類型推導","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"舉個栗子🌰:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當我們把函數返回值約束去掉以後依然可以從提示中發現返回值是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"number","attrs":{}}],"attrs":{}},{"type":"text","text":",這是因爲我們將參數約束爲","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"number","attrs":{}}],"attrs":{}},{"type":"text","text":",數字與數字相加依然是數字,所以最後函數也會返回","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"number","attrs":{}}],"attrs":{}},{"type":"text","text":",賦值給變量result,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TS","attrs":{}}],"attrs":{}},{"type":"text","text":"還會智能地發現函數返回的結果是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"number","attrs":{}}],"attrs":{}},{"type":"text","text":",所以","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"result","attrs":{}}],"attrs":{}},{"type":"text","text":"類型也是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"number","attrs":{}}],"attrs":{}},{"type":"text","text":",因此我們只需要在參數位置加上類型約束就可以了,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TS","attrs":{}}],"attrs":{}},{"type":"text","text":"在每個地方都有類型檢查,是不是很牛逼🐮","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/18/18070261e94bb2e9aeb7f871968e254f.gif","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"📢 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"緊急通知:","attrs":{}},{"type":"text","text":" 翠花小姐姐來了","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"翠花小姐姐","attrs":{}}],"attrs":{}},{"type":"text","text":"提了一個問題:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"我怎麼知道這類型推導什麼時候能推導成功,什麼時候推導失敗呢?","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"👉 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"解答:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有個小技巧,當我們看到變量或者函數的參數出現","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"三個小點","attrs":{}}],"attrs":{}},{"type":"text","text":",這","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"三個點","attrs":{}}],"attrs":{}},{"type":"text","text":"就是在提醒:你給我當心點,我確實做不到了,表示當前沒有推導出來到底是什麼類型,可以用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"any類型","attrs":{}}],"attrs":{}},{"type":"text","text":"來表示,這時就需要手動約束一下,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e7/e7c3f8b1f43ec99777ba8192fecf0c33.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"any","attrs":{}}],"attrs":{}},{"type":"text","text":":表示任意類型,對該類型,TS不進行類型檢查","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看到這,我想大家已經知道","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TS","attrs":{}}],"attrs":{}},{"type":"text","text":"有非常強大的類型檢查系統,那麼有個小問題","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"😱 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"靈魂一問:","attrs":{}},{"type":"text","text":" 請問手機號應該定義成","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"數字","attrs":{}}],"attrs":{}},{"type":"text","text":"還是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"數字字符串","attrs":{}}],"attrs":{}},{"type":"text","text":"?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"📢 如何區分數字字符串和數字,關鍵看怎麼讀?如果按照數字的方式朗讀,則爲數字,反之爲字符串","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"舉個栗子🌰:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/42/42ffda9fe71a0b82ac89ee002faa2ed0.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"帥哥一聽,唉呀媽呀,再不走桃花運被你耽擱了,趕緊拿出電話小本本,小心謹慎地生怕唸錯一個數字,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"131","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"5258","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"3720","attrs":{}}],"attrs":{}},{"type":"text","text":",平常我們都是這麼唸的,沒有人念","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"一百三十一億五千二百五十八萬三千七百二","attrs":{}}],"attrs":{}},{"type":"text","text":"吧😂,妹子要聽了,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"滾~~","attrs":{}}],"attrs":{}},{"type":"text","text":",活該你單身!😭","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2. 基本類型","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"注意是首字母是小寫","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"number:數字,","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" let figure:number = 6;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"string:字符串","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" let user:string = \"法醫\";\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"boolean:布爾值","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" let fake:boolean = false;\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"array:數組","attrs":{}},{"type":"codeinline","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":":number[]","attrs":{}}],"attrs":{}},{"type":"text","text":"這種寫法其實是語法糖,真正的寫法是下面第二種,這兩種寫法都可以約束數組的,看個人喜好,建議使用第一種,因爲在react中尖括號表示組件,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Array","attrs":{}}],"attrs":{}},{"type":"text","text":"可能會造成衝突","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" // \n let arr:number[] = [1,2,3];\n\n let arr:Array = [1,2,3];\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"object:對象","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"object","attrs":{}}],"attrs":{}},{"type":"text","text":"約束不是很常用,因爲","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"object","attrs":{}}],"attrs":{}},{"type":"text","text":"約束力不是很強,它只能約束一個對象,卻不能約束對象裏面的內容,但是有時會用到","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" //傳入一個對象,輸出value值\n function getValues(obj:object) {\n let vals = Object.values(obj);\n console.log(vals); // 輸出 法醫 18 \n }\n getValues({\n name:\"法醫\",\n age:18\n })\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"null和undefined","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"null","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"undefined","attrs":{}}],"attrs":{}},{"type":"text","text":"需要重點說一下,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"null","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"undefined","attrs":{}}],"attrs":{}},{"type":"text","text":"是所有其他類型的子類型,它們可以賦值給其它類型,但是又會發生隱患,下面方法調用都會報錯,由於約束了是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"string","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"number","attrs":{}}],"attrs":{}},{"type":"text","text":",但是值又是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"null","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"undefined","attrs":{}}],"attrs":{}},{"type":"text","text":",這種情況是我們不希望發生的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" let str:string = null;\n let nums:number = undefined;\n \n //下面都會報錯,由於約束了是string和number,但是值又是null和undefined\n str.toLocaleUpperCase();\n nums.toString();\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"👉 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"解決方案:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"在`tsconfi.json`配置文件中加上:`\"strictNullChecks\": true`之後可以獲得更加嚴格的空類型檢查,`null`和`undefined`就不能賦值給其它的了\n\n![image.png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/8dfc27334d8948708b55f257675037e7~tplv-k3u1fbpfcp-watermark.image)\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3. 其它常用類型","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"聯合類型:多種類型任選其一","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當一個變量既可以爲","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"字符串","attrs":{}}],"attrs":{}},{"type":"text","text":"又可以爲","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"undefined","attrs":{}}],"attrs":{}},{"type":"text","text":"的時候就可以使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"聯合類型","attrs":{}}],"attrs":{}},{"type":"text","text":",它可以配合使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"類型保護","attrs":{}}],"attrs":{}},{"type":"text","text":"進行判斷","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"📢 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"類型保護:","attrs":{}}],"attrs":{}},{"type":"text","text":"當對某個變量進行類型判斷之後,在判斷的語句中便可以確定它的確切類型,typeof也","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以觸發類型保護,但是它只能觸發簡單基本的類型保護,複雜類型是沒有辦法觸發的","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" let user:string | undefined;\n\n if(typeof user === \"string\"){\n //類型保護,當進入這個判斷,TS一定會知道,此時user一定是字符串\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"void類型:通常用於約束函數返回值,表示該函數沒有任何io","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"void","attrs":{}}],"attrs":{}},{"type":"text","text":"在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Js","attrs":{}}],"attrs":{}},{"type":"text","text":"也是有的,表示運算一個表達式之後返回undefined,但在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TS","attrs":{}}],"attrs":{}},{"type":"text","text":"意思是不同的,通常用於約束函數返回值,表示該函數沒有任何返回","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" function user():void{\n console.log(\"法醫\");\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"當然不約束也是可以的,因爲會`類型推導`出來\n\n![image.png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/606de9b10f71449a94cada2232cfded4~tplv-k3u1fbpfcp-watermark.image)\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"never類型:通常用於約束函數返回值,表示該函數永遠不可能結束","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" function thorwError(msg:string) {\n throw new Error(msg)\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"這個函數的`類型推導`是有問題的,推導的類型是`viod`,因爲它永遠不會結束,類型應該是`never`而不是`viod`,所以需要手動更改\n\n![GIF 2021-9-8 16-37-29.gif](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/71d428e36aaf4562a91eac8747a741cb~tplv-k3u1fbpfcp-watermark.image)\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" function thorwError(msg:string):never {\n throw new Error(msg)\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"由於是永遠不會結束,所以,下面的log函數無法執行,無法訪問代碼\n\n![image.png](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5b4441eb057046d39b3ecc18898fa130~tplv-k3u1fbpfcp-watermark.image)\n\n還有一種情況也是永遠不會結束,需要手動約束\n\n![GIF 2021-9-8 16-53-09.gif](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9377726f8fee42159d70140d0f23b654~tplv-k3u1fbpfcp-watermark.image)\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"字面量類型:使用一個","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"值","attrs":{}}],"attrs":{}},{"type":"text","text":"進行約束,而不是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"類型","attrs":{}}],"attrs":{}},{"type":"text","text":"約束","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" //表示從此以後,變量name只能是 “法醫”,別的就會報錯\n let name:\"法醫\";\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"![GIF 2021-9-8 17-18-55.gif](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5e0cde8988b24cb2b860f757236549be~tplv-k3u1fbpfcp-watermark.image)\n\n一般我們可以用字面量類型對性別或者對象中的屬性進行約束:\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" //對gender 變量進行約束,只能是男或女,其它不行\n let gender :\"男\" | \"女\";\n \n //對users對象中的name和age屬性分別約束爲字符串和數字,下次給users賦值的時候,只能包含name和age\n let users:{\n name:string\n age:number\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"元組類型(Tuple):用的不多,瞭解一下就可以了,表示固定長度的數組,並且數組中的每一項類型確定","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":" //定義了一個變量爲tupleType的數組,這個數組只能有兩項,並且第一個必須爲字符串,第二個必須爲數字\n let tupleType:[string,number];\n \n //第一項必須爲字符串,第二項必須爲數字,只能有兩項,否則報錯\n tupleType = [\"法醫\",5];\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"any類型:any類型可以繞過類型檢查,因此any類型可以賦值給任意類型,但肯定是有隱患的,因爲它無法使用TS提供的保護機制,所以不建議隨意的使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"any","attrs":{}}],"attrs":{}},{"type":"text","text":"類型,爲了解決any帶來的問題,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TS3.0","attrs":{}}],"attrs":{}},{"type":"text","text":"引入了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"unknown","attrs":{}}],"attrs":{}},{"type":"text","text":"類型","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d1/d173b23e441c895ae2b51b19d407e74b.gif","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"4. 類型別名","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"給已知的類型起個新的名字,防止重複書寫一些代碼","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"type Gender = \"男\" | \"女\";\ntype user = {\n name:string\n age:number\n gender:Gender\n}\n\nfunction getUser(g:Gender) {\n //...\n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"5. 函數的相關約束","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"函數重載","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"先看一個函數","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"combine","attrs":{}}],"attrs":{}},{"type":"text","text":",功能是如果傳遞兩個數字作爲參數的時候相乘,傳遞兩個字符串的時候相加,不相同都會報錯。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"function combine(a:number | string,b:number | string):number | string {\n if(typeof a === \"number\" && typeof b === \"number\"){\n return a * b;\n }\n else if(typeof a === \"string\" && typeof b === \"string\"){\n return a + b;\n }\n throw new Error(\"a和b必須是相同的類型\")\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"函數本身沒有什麼問題,問題就發生在函數調用的過程中,當我們代碼寫多了以後,我們也許會失誤傳遞不同的類型作爲參數,更可怕的是如果參數是函數的返回結果,那就更蒙了,因此,在函數的調用過程中最好告訴調用函數,要麼都是數字類型,要麼都是字符串類型。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/22/22628bcb51ec34ef3f2efffa0384b389.gif","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從邏輯上來說,都是數字的話返回的結果就是數字類型,都是字符串的話返回的結果就是字符串類型,然而","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"result","attrs":{}}],"attrs":{}},{"type":"text","text":"的類型是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"string | number","attrs":{}}],"attrs":{}},{"type":"text","text":",上圖可以清晰看到,這種情況,後面就沒有辦法使用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"result","attrs":{}}],"attrs":{}},{"type":"text","text":"變量了,因爲明明知道都是數字返回的結果一定是數字類型,都是字符串返回的一定是字符串類型。意味着代碼提示中不會出現所有數字擁有的方法或者所有字符串所擁有的方法,只會提示數字和字符串共同擁有的方法——","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"toString","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"valueOf","attrs":{}}],"attrs":{}},{"type":"text","text":"如下圖:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b9/b9ab8816b9c1e309ca1fea07424f7722.gif","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"👉 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"解決方案:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"加上下面兩句代碼,這兩句代碼相當於告訴 TS ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"combine函數","attrs":{}}],"attrs":{}},{"type":"text","text":"只能有兩種情況,一種是兩個數字返回數字,另一種是兩個字符串返回字符串,這兩句代碼就叫","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"函數重載","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"📢 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"函數重載:","attrs":{}}],"attrs":{}},{"type":"text","text":"在函數實現之前,對函數調用的多種情況進行聲明。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"//加上這兩句代碼\n/**\n * 得到a * b的結果\n * @param a \n * @param b \n */\nfunction combine(a:number,b:number):number;\n/**\n * 得到a + b的結果\n * @param a \n * @param b \n */\nfunction combine(a:string,b:string):string;\n\nfunction combine(a:number | string,b:number | string):number | string {\n if(typeof a === \"number\" && typeof b === \"number\"){\n return a * b;\n }\n else if(typeof a === \"string\" && typeof b === \"string\"){\n return a + b;\n }\n throw new Error(\"a和b必須是相同的類型\")\n}\n\nlet result = combine(\"b\",\"n\");\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用函數重載之後,當調用函數的時候只能傳兩個數字或者兩個字符串,否則會報錯,再來看看效果:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/95/95d5bc572e109500d8799f691b4be239.gif","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可選參數","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"📢 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"可選參數:","attrs":{}}],"attrs":{}},{"type":"text","text":"可以在某些參數名後面加上","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"?","attrs":{}}],"attrs":{}},{"type":"text","text":"號,表示該參數可以不用傳遞。可選參數必須要在參數列表的末尾","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5b/5b1df837cf9b2b87492b67370a9d89f5.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當形參爲三個,調用函數卻傳了兩個,就會報錯,TS是很嚴格的,不允許參數數量不匹配。假設第三個參數可以不傳遞,加個","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"?","attrs":{}}],"attrs":{}},{"type":"text","text":"號表示是可選參數","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/67/67f437608917cdebc07de326064d667e.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"😛 結束語","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"寫完又是深夜了,TS的基礎之基本類型檢查就寫完了,本來想着把TS基礎部分整理成","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"萬字文","attrs":{}}],"attrs":{}},{"type":"text","text":"再分享給大家的,但又覺得篇幅過長閱讀不便,那就慢慢更新吧,9月份不出意外的話基本上每天會更新一篇,有任何疑問可以留言,小夥伴們點個贊 👍 關注➕再走吧!~ 😘,我會更有動力的,晚安! 🤞","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章