北京時間7月31日,微軟宣布推出 TypeScript 3.0 正式版。TypeScript 3.0 是一個新的里程碑。官方表示,盡管版本號是一個大變化,但 3.0 版本幾乎沒有破壞性的變更(這意味著我們可以很輕松地升級到該版本),3.0 引入了一種新的靈活且可擴展的方式來構建項目、提供對參數列表進行操作新的強大支持能力、強制顯式檢查的新類型、更好的 JSX 支持、整體更好的錯誤處理,等等。
下面簡單說說值得關注的更新,更詳細的更新說明請移步至發布說明。
項目引用(Project references)
這是 3.0 版本中引入的一個新概念。考慮這樣一個場景 —— 對于開發者而言,為庫或應用程序提供幾個不同的構建步驟是相當常見的。也許你的代碼庫有一個 src 和一個測試目錄。也許你將前端代碼放在一個名為 client 的文件夾中,將 Node.js 后端代碼放在名為 server 的文件夾中,每個文件夾都從共享文件夾中導入代碼。也許你使用所謂的“monorepo”并且有許多項目以復雜的方式相互依賴。
官方表示,針對上述的場景,他們為 TypeScript 3.0 開發了最重大的功能之一 ——“項目引用(project references)”。
項目引用允許 TypeScript 項目依賴于其他 TypeScript 項目 —— 特別是允許 tsconfig.json 文件引用其他 tsconfig.json 文件。指定這些依賴項可以更容易地將代碼拆分為更小的項目,因為它為 TypeScript(及其周圍的工具)提供了一種理解構建順序和輸出結構的方法。這意味著更快的構建可以逐步工作,并支持跨項目導航、編輯和重構。由于 3.0 奠定了基礎并公開了 API,因此任何構建工具都能夠提供這一功能。
在JSX 中支持 defaultProps
該特性使得調用者可以通過不需要某些參數來更輕松地使用函數。
破壞性變更API 的破壞性變更
The deprecated internal method LanguageService#getSourceFile has been removed, as it has been deprecated for two years. See #24540.
The deprecated function TypeChecker#getSymbolDisplayBuilder and associated interfaces have been removed. See #25331. The emitter and node builder should be used instead.
The deprecated functions escapeIdentifier and unescapeIdentifier have been removed. Due to changing how the identifier name API worked in general, they have been identity functions for a few releases, so if you need your code to behave the same way, simply removing the calls should be sufficient. Alternatively, the typesafe escapeLeadingUnderscores and unescapeLeadingUnderscores should be used if the types indicate they are required (as they are used to convert to or from branded __String and string types).
The TypeChecker#getSuggestionForNonexistentProperty, TypeChecker#getSuggestionForNonexistentSymbol, and TypeChecker#getSuggestionForNonexistentModule methods have been made internal, and are no longer part of our public API. See #25520.
相關鏈接:
發布說明 https://blogs.msdn.microsoft.com/typescript/2018/07/30/announcing-typescript-3-0/
TypeScript 3.0 新特性 https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript
破壞性變化 https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes