北京時(shí)間7月31日,微軟宣布推出 TypeScript 3.0 正式版。TypeScript 3.0 是一個(gè)新的里程碑。官方表示,盡管版本號(hào)是一個(gè)大變化,但 3.0 版本幾乎沒有破壞性的變更(這意味著我們可以很輕松地升級(jí)到該版本),3.0 引入了一種新的靈活且可擴(kuò)展的方式來(lái)構(gòu)建項(xiàng)目、提供對(duì)參數(shù)列表進(jìn)行操作新的強(qiáng)大支持能力、強(qiáng)制顯式檢查的新類型、更好的 JSX 支持、整體更好的錯(cuò)誤處理,等等。
下面簡(jiǎn)單說(shuō)說(shuō)值得關(guān)注的更新,更詳細(xì)的更新說(shuō)明請(qǐng)移步至發(fā)布說(shuō)明。
項(xiàng)目引用(Project references)
這是 3.0 版本中引入的一個(gè)新概念??紤]這樣一個(gè)場(chǎng)景 —— 對(duì)于開發(fā)者而言,為庫(kù)或應(yīng)用程序提供幾個(gè)不同的構(gòu)建步驟是相當(dāng)常見的。也許你的代碼庫(kù)有一個(gè) src 和一個(gè)測(cè)試目錄。也許你將前端代碼放在一個(gè)名為 client 的文件夾中,將 Node.js 后端代碼放在名為 server 的文件夾中,每個(gè)文件夾都從共享文件夾中導(dǎo)入代碼。也許你使用所謂的“monorepo”并且有許多項(xiàng)目以復(fù)雜的方式相互依賴。
官方表示,針對(duì)上述的場(chǎng)景,他們?yōu)?TypeScript 3.0 開發(fā)了最重大的功能之一 ——“項(xiàng)目引用(project references)”。
項(xiàng)目引用允許 TypeScript 項(xiàng)目依賴于其他 TypeScript 項(xiàng)目 —— 特別是允許 tsconfig.json 文件引用其他 tsconfig.json 文件。指定這些依賴項(xiàng)可以更容易地將代碼拆分為更小的項(xiàng)目,因?yàn)樗鼮?TypeScript(及其周圍的工具)提供了一種理解構(gòu)建順序和輸出結(jié)構(gòu)的方法。這意味著更快的構(gòu)建可以逐步工作,并支持跨項(xiàng)目導(dǎo)航、編輯和重構(gòu)。由于 3.0 奠定了基礎(chǔ)并公開了 API,因此任何構(gòu)建工具都能夠提供這一功能。
在JSX 中支持 defaultProps
該特性使得調(diào)用者可以通過不需要某些參數(shù)來(lái)更輕松地使用函數(shù)。
破壞性變更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.
相關(guān)鏈接:
發(fā)布說(shuō)明 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