久久九九热re6这里只有精品,国产亚洲欧洲精品,欧美在线综合在线,国产精品一区二区无线

<menuitem id="3wyru"></menuitem>
  • 
    
    <td id="3wyru"><menuitem id="3wyru"></menuitem></td>

    <address id="3wyru"></address>
  • 環(huán)球熱議:函數(shù)返回值類型優(yōu)化,JSX 增強,TypeScript 5.1 發(fā)布!

    時間:2023-06-09 06:10:56 來源: CSDN


    譯者 | 禾木木 責(zé)編 | 夏萌

    出品 | CSDN(ID:CSDNnews)


    (資料圖片僅供參考)

    近日,微軟宣布正式發(fā)布 TypeScript 5.1 版本。

    如果你還不熟悉 TypeScript,它是一種建立在 JavaScript 基礎(chǔ)上的語言,通過添加靜態(tài)類型語法來構(gòu)建的語言。這些類型可以描述我們程序的一些細節(jié),可以使用 TypeScript 對代碼進行類型檢查,并在運行代碼之前告訴你代碼錯誤的相關(guān)信息。

    此外,你還可以使用 TypeScript 編譯器從代碼中剝離類型,并為你提供可在任何地方運行的簡潔易讀的 JavaScript 代碼。除了類型檢查之外,TypeScript 還使用靜態(tài)類型來支持強大的編輯器工具,例如自動完成、代碼導(dǎo)航、重構(gòu)等。

    如果你在 Visual Studio Code 或 Visual Studio 這樣的編輯器中使用過 JavaScript,那么你已經(jīng)用上了類型和 TypeScript 帶來的體驗。

    如果你已經(jīng)在項目中使用 TypeScript,則可以通過 NuGet 獲取它,也可以通過以下命令使用 npm 獲?。?/p>

    npm install -D typescript

    以下是 TypeScript 5.1 版本中新增的主要功能:

    更容易實現(xiàn) undefined 函數(shù)的返回值類型

    getter 和 setter 支持設(shè)置不同類型

    JSX 元素和 JSX 標(biāo)簽類型之間解耦類型檢查

    帶命名空間的 JSX 標(biāo)簽

    typeRoots 在模塊解析中被查詢

    JSX 標(biāo)簽支持鏈接光標(biāo)

    @Param JSDoc 標(biāo)簽中支持代碼補全

    優(yōu)化

    重大變更

    Beta 版本和 RC 版有哪些新變化?

    Beta 版以后,糾正了裝飾器中 init 鉤子的一些行為,并作出調(diào)整。還對 isolatedModules 下的 emit 行為進行了修改,以確保腳本文件不會被重寫為模塊。這也意味著使用 transpileModule API 不會將腳本文件被解釋為模塊,因為它假定使用了 isolatedModules。

    RC 版以后,對內(nèi)置重構(gòu)進行了輕微的迭代。但,實現(xiàn)仍然需要一些改進。因此,你現(xiàn)在可能無法在大多數(shù)編輯器中訪問它,只能通過使用 TypeScript 的夜間版本來選擇。預(yù)計 TypeScript 5.2 或 TypeScript 5.1 的未來補丁版本將重新引入這種重構(gòu)。

    更容易實現(xiàn) undefined 函數(shù)的返回值類型

    在 JavaScript 中,如果函數(shù)在執(zhí)行過程中沒有返回值,就會返回 undefined:

    function foo ( ) { // no return}

    // x = undefinedlet x = foo ( ) ;

    然而,在以前版本的 TypeScript 中,只有返回值類型為 void 和 any 的函數(shù)可以沒有 return 語句。這意味著即使明確知道這個函數(shù)是返回 undefined 的,也需要至少有一個 return 語句。

    // fine - we inferred that "f1" returns "void"function f1 ( ) { // no returns}

    // fine - "void" doesn"t need a return statementfunction f2 ( ) : void { // no returns}

    // fine - "any" doesn"t need a return statementfunction f3 ( ) : any { // no returns}

    // error!// A function whose declared type is neither "void" nor "any" must return a value.function f4 ( ) : undefined { // no returns}

    如果某個 API 希望函數(shù)返回 undefined。在以前的版本中,可能需要顯式返回一個 undefined 或者顯式添加一個 return 語句。

    declare function takesFunction ( f: ( ) => undefined ) : undefined;

    // error! // Argument of type " ( ) => void" is not assignable to parameter of type " ( ) => undefined".takesFunction ( ( ) => { // no returns} ) ;

    // error!// A function whose declared type is neither "void" nor "any" must return a value.takesFunction ( ( ) : undefined => { // no returns} ) ;

    // error!// Argument of type " ( ) => void" is not assignable to parameter of type " ( ) => undefined".takesFunction ( ( ) => { return;} ) ;

    // workstakesFunction ( ( ) => { return undefined;} ) ;

    // works takesFunction ( ( ) : undefined => { return;} ) ;

    這種行為令人困惑,尤其是在調(diào)用無法控制的函數(shù)時。理解推斷 void 和 undefined 之間的相互作用,以及一個返回 undefined 的函數(shù)是否需要一個 return 語句等。

    在 TypeScript 5.1 中,允許返回 undefined 的函數(shù)沒有 return 語句。如下所示:

    // worksfunction f4 ( ) : undefined { // no returns}// workstakesFunction ( ( ) : undefined => { // no returns} ) ;

    如果函數(shù)沒有 return 并且被傳遞給期望返回 undefined 的函數(shù)參數(shù),TypeScript 會推斷該函數(shù)的返回類型為 undefined。

    // Works in TypeScript 5.1!takesFunction ( function f ( ) { // ^ return type is undefined // no returns} ) ;// Works in TypeScript 5.1!takesFunction ( function f ( ) { // ^ return type is undefined return;} ) ;

    為了解決另一個類似的痛點,在 TypeScript 的 --noImplicitReturns 選項下,僅返回 undefined 的函數(shù)現(xiàn)在具有與 void 類似的異常,因為并非每個代碼路徑都必須以顯式 return 結(jié)束。

    // Works in TypeScript 5.1 under "--noImplicitReturns"!function f ( ) : undefined { if ( Math.random ( ) ) { // do some stuff... return; }}

    getter 和 setter 支持設(shè)置不同類型

    TypeScript 4.3 使得 get 和 set 訪問器對可以指定兩種不同的類型成為可能。

    interface Serializer { set value ( v: string | number | boolean ) ; get value ( ) : string;}

    declare let box: Serializer;

    // Allows writing a "boolean"box.value = true;

    // Comes out as a "string"console.log ( box.value.toUpperCase ( ) ) ;

    最初要求 get 類型必須是 set 類型的子類型。意味著以下的代碼是合理的:

    box.value = box.value;

    但是,有許多現(xiàn)有的和提議的 API 在它們的 getter 和 setter 之間具有完全不相關(guān)的類型。

    TypeScript 5.1 現(xiàn)在允許 get 和 set 訪問器屬性設(shè)置不同的類型,前提是它們具有顯式類型注釋。雖然此版本的 TypeScript 尚未更改這些內(nèi)置接口的類型,但現(xiàn)在可以通過以下方式定義:

    interface CSSStyleRule { get style ( ) : CSSStyleDeclaration; set style ( newValue: string ) ;}

    也允許其他模式的使用,但如果某些基礎(chǔ)狀態(tài)尚未初始化,則訪問器可以返回。

    class SafeBox { #value: string | undefined;

    // 只接受字符串 set value ( newValue: string ) {

    // 必須檢查 undefined get value ( ) : string | undefined { return this.#value; }}

    實際上,這與在 --exactOptionalProperties 下檢查可選屬性的方式類似。

    JSX 元素和 JSX 標(biāo)簽類型之間解耦類型檢查

    TypeScript 在處理 JSX 時的一個痛點是對每個 JSX 元素標(biāo)簽類型的要求。而這個版本使得 JSX 庫能夠更準(zhǔn)確地描述 JSX 組件的返回類型。對于很多人來說,這具體意味著可以在 React 中使用異步服務(wù)器組件。

    例如,有以下 JSX 元素:

    // A self-closing JSX tag

    // A regular element with an opening/closing tag

    當(dāng)對 進行類型檢查時,TypeScript 總是會查找名為 JSX 的命名空間,并從中獲取一個名為 Element 的類型,也就是在查找 JSX.Element。

    但是,為了檢查 Foo 或 Bar 本身是否是有效的標(biāo)簽名稱,TypeScript 會粗略地獲取由 Foo 或 Bar 返回或構(gòu)造的類型,并檢查與 JSX.Element 的兼容性(或者如果類型是可構(gòu)造的,則檢查另一種稱為 JSX.ElementClass 的類型)。

    這個限制意味著如果組件返回比 JSX.Element 更廣泛的類型,則無法使用組件。

    舉一個具體的例子,React 的未來版本提議對返回 Promise 的組件提供有限支持,但是現(xiàn)有版本的 TypeScript 無法表達這種類型,除非徹底放寬 JSX.Element 的類型限制。

    import * as React from "react";

    async function Foo ( ) { return

    ;}let element = ;// ~~~// "Foo" cannot be used as a JSX component.// Its return type "Promise" is not a valid JSX element.

    為了向庫提供一種表達方式,TypeScript 5.1 現(xiàn)在會查找名為 JSX.ElementType 的類型。ElementType 精確指定什么可以有效用作 JSX 元素中的標(biāo)簽。因此,它可能會被定義為類似于以下這樣:

    namespace JSX { export type ElementType = // All the valid lowercase tags keyof IntrinsicAttributes // Function components ( props: any ) => Element // Class components new ( props: any ) => ElementClass;

    export interface IntrinsictAttributes extends /*...*/ {} export type Element = /*...*/; export type ClassElement = /*...*/;}

    命名空間的 JSX 標(biāo)簽

    在使用 JSX 時,可以使用帶命名空間的屬性名。

    import * as React from "react";

    // Both of these are equivalent:const x = ;const y = ;

    interface FooProps { "a:b": string;}

    function Foo ( props: FooProps ) { return

    {props [ "a:b" ] }
    ;}

    當(dāng)名稱的第一部分是小寫字母時,JSX.IntrinsicAttributes 上的命名空間屬性名會以類似的方式進行查找。

    // In some library"s code or in an augmentation of that library:namespace JSX { interface IntrinsicElements { [ "a:b" ] : { prop: string }; }}

    // In our code:let x = ;

    typeRoots 在模塊解析中被查詢

    當(dāng) TypeScript 指定的模塊查找策略無法解析路徑時,它現(xiàn)在將解析相對于指定 typeRoots 的包。

    引入 JSX 標(biāo)簽中鏈接光標(biāo)

    TypeScript 現(xiàn)在支持 JSX 標(biāo)簽名稱的鏈接編輯。允許編輯器自動同時編輯多個位置。

    這項新功能應(yīng)該適用于 TypeScript 和 JavaScript 文件,并且可以在 Visual Studio Code Insiders 中啟用。在 Visual Studio Code 中,勾選 Editor: Linked Editing 選項即可啟用:

    或者在 JSON 設(shè)置文件中配置 editor.linkedEditing:

    { // ... "editor.linkedEditing": true,}

    Visual Studio 17.7 Preview 1 也將支持此功能。

    @Param JSDoc 標(biāo)簽中支持代碼補全

    在 TypeScript 和 JavaScript 文件中輸入 @param 標(biāo)簽時,TypeScript 現(xiàn)在提供代碼補全。

    優(yōu)化

    避免不必要的類型實例化

    TypeScript 5.1 現(xiàn)在避免在已知不包含對外部類型參數(shù)的引用的對象類型中執(zhí)行類型實例化。這有可能減少許多不必要的計算,并將 material-ui 的文檔目錄的類型檢查時間減少 50% 以上。

    通過減少不必要的類型實例化,TypeScript 5.1 在類型檢查方面提供了更高的效率。尤其是在具有復(fù)雜類型層級或大型代碼庫的情況下,可以顯著提高類型檢查的性能。

    聯(lián)合字面量類型檢查優(yōu)化

    在檢查源類型是否屬于聯(lián)合類型時,TypeScript 首先將使用該源類型的內(nèi)部類型標(biāo)識符進行快速查找。如果查找失敗,則 TypeScript 會針對聯(lián)合類型中的每種類型檢查其兼容性。當(dāng)將字面類型與僅包含字面類型的聯(lián)合類型相關(guān)聯(lián)時,TypeScript 現(xiàn)在可以避免針對聯(lián)合類型中的每種其他類型進行完整遍歷。

    這種優(yōu)化可以將此問題中的代碼的類型檢查時間從約 45 秒減少到約 0.4 秒。

    在解析 JSDoc 時,減少對掃描器的調(diào)用

    在舊版本的 TypeScript 中,解析 JSDoc 注釋時會使用掃描器 / 標(biāo)記器將注釋分解為細粒度的標(biāo)記,并將內(nèi)容重新組合。這在規(guī)范化注釋文本方面可能是有幫助的,例如多個空格會被合并為一個空格,但這種方法會導(dǎo)致解析器和掃描器之間頻繁的跳轉(zhuǎn),增加了 JSDoc 解析的開銷。

    TypeScript 5.1 對將 JSDoc 注釋分解為掃描器 / 分詞器中的內(nèi)容更改了邏輯。掃描器現(xiàn)在將更大的內(nèi)容塊直接返回給解析器,以便根據(jù)需要進行處理。

    這些更改已將幾個 10Mb 的主要是注釋的 JavaScript 文件的解析時間縮短了大約一半。

    重大變更

    ES2020 和 Node.js 14.17 作為最低運行時要求

    TypeScript 5.1 現(xiàn)在包含了在 ECMAScript 2020 中引入的 JavaScript 功能。因此,TypeScript 至少需要在一個相對較新的運行時環(huán)境下運行。對于大多數(shù)用戶來說,這意味著 TypeScript 現(xiàn)在只能在 Node.js 14.17 及更高版本上運行。

    如果你嘗試在較舊版本的 Node.js(如 Node 10 或 12)上運行 TypeScript 5.1,可能會遇到以下錯誤,無論是運行 tsc.js 還是 tsserver.js:

    node_modules/typescript/lib/tsserver.js:2406 for ( let i = startIndex ?? 0; i < array.length; i++ ) { ^SyntaxError: Unexpected token "?" at wrapSafe ( internal/modules/cjs/loader.js:915:16 ) at Module._compile ( internal/modules/cjs/loader.js:963:27 ) at Object.Module._extensions..js ( internal/modules/cjs/loader.js:1027:10 ) at Module.load ( internal/modules/cjs/loader.js:863:32 ) at Function.Module._load ( internal/modules/cjs/loader.js:708:14 ) at Function.executeUserEntryPoint [ as runMain ] ( internal/modules/run_main.js:60:12 ) at internal/main/run_main_module.js:17:47

    此外,如果你嘗試安裝 TypeScript,你將會收到類似以下的錯誤信息:

    npm WARN EBADENGINE Unsupported engine {npm WARN EBADENGINE package: "typescript@5.1.3",npm WARN EBADENGINE required: { node: ">=14.17" },npm WARN EBADENGINE current: { node: "v12.22.12", npm: "8.19.2" }npm WARN EBADENGINE }

    來自 Yarn:

    error typescript@5.1.3: The engine "node" is incompatible with this module. Expected version ">=14.17". Got "12.22.12"error Found incompatible module.

    顯式指定了 typeRoots 后,將禁用對 node_modules/@types 的向上查找

    以前,在 tsconfig.json 中指定了 typeRoots 選項但無法解析到任何 typeRoots 目錄時,TypeScript 仍然會繼續(xù)向上遍歷父目錄,嘗試在每個父目錄的 node_modules/@types 文件夾中解析包。

    這種行為可能導(dǎo)致過多的查找,并且在 TypeScript 5.1 中已被禁用。因此,您可能會開始看到類似以下錯誤的錯誤,這是基于您的 tsconfig.json 中的 types 選項或 /// 指令的條目而產(chǎn)生的:

    error TS2688: Cannot find type definition file for "node".error TS2688: Cannot find type definition file for "mocha".error TS2688: Cannot find type definition file for "jasmine".error TS2688: Cannot find type definition file for "chai-http".error TS2688: Cannot find type definition file for "webpack-env"".

    解決方案通常是在您的 typeRoots 中添加針對 node_modules/@types 的特定條目:

    { "compilerOptions": { "types": [ "node", "mocha" ] , "typeRoots": [ // Keep whatever you had around before. "./some-custom-types/", // You might need your local "node_modules/@types". "./node_modules/@types", // You might also need to specify a shared "node_modules/@types" // if you"re using a "monorepo" layout. "../../node_modules/@types", ] }}

    以上就是 TypeScript 5.1 的穩(wěn)定版本所更新的內(nèi)容,此外,微軟項目團隊已經(jīng)在努力開發(fā) TypeScript 5.2 版本,如果你想知道更多關(guān)于 TypeScript 5.1 版本的信息,可查看發(fā)布說明。

    參考鏈接:

    https://devblogs.microsoft.com/typescript/announcing-typescript-5-1/

    關(guān)鍵詞:

    網(wǎng)站簡介 網(wǎng)站團隊 本網(wǎng)動態(tài) 友情鏈接 版權(quán)聲明 我要投稿

    Copyright? 2014-2020 中原網(wǎng)視臺(ju8hn6.cn) All rights reserved.