解釋 TypeScript 中 never 型別的用途
TypeScript 是一種型別嚴格的語言,我們需要為每個變數定義型別。此外,我們還需要定義函式的返回值型別以及函式引數的型別。
never 也是 TypeScript 中的一種型別,就像其他資料型別(如字串、數字、布林值、符號等)一樣。我們可以使用 `never` 關鍵字來建立一個 never 型別的變數。
使用者可以在確信任何情況永遠不會發生時使用 never 型別。例如,當我們確定函式永遠不會返回值時,可以使用 never 作為返回值型別。
語法
使用者可以按照以下語法使用 never 型別作為變數的字面量。
// never type as a variable literal let variable: never; // never type as a function return type function func(): never { // write any condition such that the function shouldn't return any value }
在上述語法中,變數型別為 never,這意味著我們永遠無法向變數儲存任何值。此外,函式的返回值型別為 never,這意味著我們永遠無法從函式返回值。
示例 1:使用 never 關鍵字作為字面量
在下面的示例中,我們定義了變數並使用 never 關鍵字作為字面量。之後,使用者可以看到我們無法為 never 型別的變數賦值。如果嘗試為 never 型別的變數賦值,TypeScript 編譯器會生成錯誤。
// never type as a variable literal let variable: never; // The below code generates the error message like Type 'number' is not assignable to type 'never'. // variable = 10; // we can't print the variable before initializing it. // console.log(variable); // This will not work as we can't assign the value to the variable of never type // let variable2: never = 10; console.log("Working the variable of type never")
編譯後,它將生成以下 JavaScript 程式碼:
// never type as a variable literal var variable; // The below code generates the error message like Type 'number' is not assignable to type 'never'. // variable = 10; // we can't print the variable before initializing it. // console.log(variable); // This will not work as we can't assign the value to the variable of never type // let variable2: never = 10; console.log("Working the variable of type never");
以上程式碼將產生以下輸出:
輸出
Working the variable of type never
示例 2:使用 "never" 作為函式的返回值型別
在下面的示例中,我們建立了 `sample` 和 `test` 函式。`sample` 函式無限次執行 while 迴圈,並且永遠不會停止。`test` 函式也無限次執行 for 迴圈,因此它永遠不會返回值,我們可以使用 never 作為函式的返回值型別。
此外,使用者可以看到 TypeScript 永遠不會呼叫 `sample()` 函式,因為 `test()` 函式的執行永遠不會停止。
// defining the sample function, which never returns a value and never stops execution function sample(param1: string): never { while (true) { console.log("The value of the param1 is " + param1); } } // test function, which will return for loop for infinite time function test(param2: number): never { for (let i = 0; ; ) { console.log("The value of param2 is " + param2); } } // calling the test function test(20); // calling the sample function // sample function never invoked, as the test function will never stop sample("Hello");
編譯後,它將生成以下 JavaScript 程式碼:
// defining the sample function, which never returns a value and never stops execution function sample(param1) { while (true) { console.log("The value of the param1 is " + param1); } } // test function, which will return for loop for infinite time function test(param2) { for (var i = 0;;) { console.log("The value of param2 is " + param2); } } // calling the test function test(20); // calling the sample function // sample function never invoked, as the test function will never stop sample("Hello");
示例 3
在這個示例中,我們建立了 `error` 函式,它總是丟擲錯誤。因此,它永遠不會從函式返回值。之後,我們從 `sample()` 函式呼叫了 `error()` 函式。
在輸出中,使用者可以看到以下程式碼顯示錯誤。
// function that always throws an error function error(): never { throw new Error("Errors in the return type!"); } // calling the error() function from the sample() function function sample() { return error(); } sample();
編譯後,它將生成以下 JavaScript 程式碼:
// function that always throws an error function error() { throw new Error("Errors in the return type!"); } // calling the error() function from the sample() function function sample() { return error(); } sample();
以上程式碼將產生以下輸出:
輸出
Error: Errors in the return type!
將 never 型別與其他資料型別聯合或交叉使用
我們可以將 never 型別與其他資料型別(如數字、字串或布林值)一起用作字面量。如果我們獲取 never 型別和數字型別的聯合,則變數變為數字型別。
如果我們獲取 never 型別和數字型別的交叉,則 never 型別始終覆蓋,並且變數變為 never 型別。
語法
使用者可以按照以下語法使用 never 型別作為其他資料型別的聯合和交叉。
let var1: never | number; let var2: never & number;
在上述語法中,`var1` 既可以是 never 型別也可以是數字型別,而 `var2` 是 never 型別和數字型別的交叉,這意味著 never 型別始終覆蓋數字型別。
示例 4
在下面的示例中,使用者可以看到我們可以為 `var1` 分配數字值,因為數字型別覆蓋了 never 型別。此外,我們無法為 `var2` 分配值,因為 never 型別覆蓋了數字資料型別。
let var1: never | number; let var2: never & number; // we can assign value to the var1 as it is a type of number var1 = 30; // we can't assign value to the var2 as it is a type of never // var2 = 30; console.log("The value of var1 is " + var1);
編譯後,它將生成以下 JavaScript 程式碼:
var var1; var var2; // we can assign value to the var1 as it is a type of number var1 = 30; // we can't assign value to the var2 as it is a type of never // var2 = 30; console.log("The value of var1 is " + var1);
以上程式碼將產生以下輸出:
The value of var1 is 30
在本教程中,使用者學習了 TypeScript 中的 never 型別。我們透過不同的示例瞭解了 never 型別的不同用例。當我們將 never 用作函式的返回值型別時,該函式應包含任何條件,這些條件永遠不允許函式返回值。