
- TypeScript 基礎
- TypeScript - 首頁
- TypeScript - 路線圖
- TypeScript - 概述
- TypeScript - 環境搭建
- TypeScript - 基本語法
- TypeScript vs. JavaScript
- TypeScript - 特性
- TypeScript - 變數
- TypeScript - let & const
- TypeScript - 運算子
- TypeScript 基本型別
- TypeScript - 型別
- TypeScript - 型別註解
- TypeScript - 型別推斷
- TypeScript - 數字
- TypeScript - 字串
- TypeScript - 布林值
- TypeScript - 陣列
- TypeScript - 元組
- TypeScript - 列舉
- TypeScript - any 型別
- TypeScript - never 型別
- TypeScript - 聯合型別
- TypeScript - 字面量型別
- TypeScript - Symbol
- TypeScript - null vs. undefined
- TypeScript - 類型別名
- TypeScript 控制流
- TypeScript - 決策語句
- TypeScript - if 語句
- TypeScript - if else 語句
- TypeScript - 巢狀 if 語句
- TypeScript - switch 語句
- TypeScript - 迴圈
- TypeScript - for 迴圈
- TypeScript - while 迴圈
- TypeScript - do while 迴圈
- TypeScript 函式
- TypeScript - 函式
- TypeScript - 函式型別
- TypeScript - 可選引數
- TypeScript - 預設引數
- TypeScript - 匿名函式
- TypeScript - Function 建構函式
- TypeScript - rest 引數
- TypeScript - 引數解構
- TypeScript - 箭頭函式
- TypeScript 介面
- TypeScript - 介面
- TypeScript - 介面擴充套件
- TypeScript 類和物件
- TypeScript - 類
- TypeScript - 物件
- TypeScript - 訪問修飾符
- TypeScript - 只讀屬性
- TypeScript - 繼承
- TypeScript - 靜態方法和屬性
- TypeScript - 抽象類
- TypeScript - 存取器
- TypeScript - 鴨子型別
- TypeScript 高階型別
- TypeScript - 交叉型別
- TypeScript - 型別守衛
- TypeScript - 型別斷言
- TypeScript 型別操作
- TypeScript - 從型別建立型別
- TypeScript - keyof 型別運算子
- TypeScript - typeof 型別運算子
- TypeScript - 索引訪問型別
- TypeScript - 條件型別
- TypeScript - 對映型別
- TypeScript - 模板字面量型別
- TypeScript 泛型
- TypeScript - 泛型
- TypeScript - 泛型約束
- TypeScript - 泛型介面
- TypeScript - 泛型類
- TypeScript 其他
- TypeScript - 三斜線指令
- TypeScript - 名稱空間
- TypeScript - 模組
- TypeScript - 環境宣告
- TypeScript - 裝飾器
- TypeScript - 型別相容性
- TypeScript - Date 物件
- TypeScript - 迭代器和生成器
- TypeScript - Mixin
- TypeScript - 實用型別
- TypeScript - 裝箱和拆箱
- TypeScript - tsconfig.json
- 從 JavaScript 到 TypeScript
- TypeScript 有用資源
- TypeScript - 快速指南
- TypeScript - 有用資源
- TypeScript - 討論
TypeScript - never 型別
never 型別
TypeScript 中的 never 型別表示永遠不會出現的數值。例如,丟擲錯誤的函式的返回型別是 never。
function funcName(): never{ // it throws an exception or never returns }
你不能將值賦給 never 型別的變數 –
let x: never; x = 10; // throw error
上面的 TypeScript 程式碼片段將丟擲以下錯誤 –
Type 'number' is not assignable to type 'never'.
你可以使用 never 型別作為永遠不會返回或總是丟擲異常的函式的返回型別。
一個永遠不會停止執行的函式 –
function infiniteLoop(): never{ for(;;){} }
另一個永遠不會停止執行的函式示例 –
function infiniteLoop(): never{ while (true) { } }
當被任何永遠不會為真的型別守衛縮小時,變數也會獲得 never 型別。
當我們已經窮盡所有可能的值並且我們沒有任何東西可以賦值時,使用 never 型別。
function check(value: string | number) { if (typeof value === 'string') { return "value is string"; } else { return "value is number"; } // here, not a string or number // "value" can't occur here, so it's type "never" }
never 型別是每個型別的子型別,並且可以賦值給每個型別;但是,沒有型別是 never 的子型別,或者可以賦值給 never(除了 never 本身)。甚至 any 也不能賦值給 never。
你可以用 never 型別註釋變數,但這很不常見 –
function showError(): never{ throw new Error("Error message from function with never as return type."); } let x: never = showError();
在上面的 Typescript 程式碼片段中,變數 x 用 never 型別註釋。變數 x 被賦值 showError() 函式,該函式本身的返回型別為 never。
void vs. never
在 TypeScript 中,void 型別的變數可以儲存 undefined 作為值。另一方面,never 不能儲存任何值。
let val1: void = undefined; let val2: never = undefined; // error: Type 'undefined' is not assignable to type 'never'.
我們知道,如果 TypeScript 中的函式不返回值,則返回 undefined。我們通常用 void 來註釋此類函式的返回型別。看下面的例子,
function greet(): void{ console.log("Welcome to tutorials Point"); } let msg: void = greet(); console.log(msg);
在上面的例子中,我們定義了一個不返回值的函式 greet()。當我們呼叫該函式時,返回值是 undefined。
編譯後,它將生成以下 JavaScript 程式碼。
function greet() { console.log("Welcome to tutorials Point"); } let msg = greet(); console.log(msg);
上述程式碼的輸出如下:
Undefined
廣告