如何編寫健壯的TypeScript庫?

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當你用TypeScript編寫庫時,你通常不知道這個庫最終將如何被使用。即使你"},{"type":"link","attrs":{"href":"https:\/\/github.com\/true-myth\/true-myth#design-philosophy","title":"","type":null},"content":[{"type":"text","text":"警告"}]},{"type":"text","text":"潛在用戶,你編寫這個庫只是針對TypeScript用戶,你還是可能會在某個時刻擁有JavaScript用戶——或者是因爲他們不顧你的警告而使用這個庫,或者是他們因爲"},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Transitive_dependency","title":"","type":null},"content":[{"type":"text","text":"傳遞性依賴"}]},{"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":"其主要部分是函數定義和函數體。如果你針對一個純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":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"interface Person {\n age: number;\n name?: string;\n}\n\n\nfunction describe(person: Person): string {\n let name = person.name ?? 'someone';\n return `${name} is ${person.age} years old!`;\n}"}]},{"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":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"describe({ name: \"chris\" })"}]},{"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":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"describe(\"potato\");"}]},{"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":"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}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你的庫的JS用戶並不是故意這麼做的。恰恰相反!在任何足夠大的系統中,很"},{"type":"text","marks":[{"type":"italic"}],"text":"容易"},{"type":"text","text":"將錯誤的參數傳遞給系統中的某個函數。這通常是一個很難避免的錯誤,比如在一個點上做了修改,許多其它地方需要更新,但漏掉了一個點。故意的JS開發者會把壞數據發送到你設計精美的TS API中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"text","text":"如果你針對一個純TypeScript讀者編寫,那麼你只需定義函數類型並信任編譯器處理其它事情"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章