如何編寫健壯的 TypeScript 庫?

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當你用 TypeScript 編寫庫時,你通常不知道這個庫最終將如何被使用。即使你 警告潛在用戶,你編寫這個庫只是針對 TypeScript 用戶,你還是可能會在某個時刻擁有 JavaScript 用戶——或者是因爲他們不顧你的警告而使用這個庫,或者是他們因爲傳遞性依賴而使用這個庫。這有一個非常重要的後果:你必須將這個庫設計成任何語言的開發者都可以使用!"}]},{"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":"其主要部分是函數定義和函數體。如果你針對一個純 TypeScript 讀者編寫,那麼你只需定義函數類型並信任編譯器處理其它事情。如果你針對一個純 JavaScrpit 讀者編寫,那麼你需要記錄那些類型,但在函數中將實際的類型設爲"},{"type":"codeinline","content":[{"type":"text","text":"unknown"}]},{"type":"text","text":"並檢查調用方傳遞的內容。"}]},{"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":"例如,給定如下代碼"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"interface Person {"}]},{"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":"一個 JS 用戶可能用任何東西來調用"},{"type":"codeinline","content":[{"type":"text","text":"describe"}]},{"type":"text","text":"函數。"}]},{"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":"正確寫法:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"describe({ name: \"chris\" })"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"災難性的錯誤寫法:"}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"describe(\"potato\");"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最常見的 JS 錯誤:"}]},{"type":"bulletedlist"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"describe(undefined);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你的庫的 JS 用戶並不是故意這麼做的。恰恰相反,在任何足夠大的系統中,很容易將錯誤的參數傳遞給系統中的某個函數。這通常是一個很難避免的錯誤,比如在一個點上做了修改,許多其它地方需要更新,但漏掉了一個點。故意的 JS 開發者會把壞數據發送到你設計精美的 TS API 中。"}]},{"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":"如果你針對一個純 TypeScript 讀者編寫,那麼你只需定義函數類型並信任編譯器處理其它事情"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章