
- 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 - 函式構造器
- 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 - Mixins
- TypeScript - 實用工具型別
- TypeScript - 裝箱和拆箱
- TypeScript - tsconfig.json
- 從 JavaScript 到 TypeScript
- TypeScript 有用資源
- TypeScript - 快速指南
- TypeScript - 有用資源
- TypeScript - 討論
TypeScript - typeof 型別運算子
在 TypeScript 中,typeof 是一個非常有用的運算子和關鍵字,用於檢查變數或識別符號的型別。然而,TypeScript 是一種型別嚴格的語言。因此,我們在定義識別符號本身時需要定義變數或物件的型別。儘管如此,有時我們仍然需要檢查變數的型別。
例如,我們透過表單從使用者那裡獲取資料,並透過 TypeScript 處理它。然後,我們必須驗證資料並執行一些資料庫操作。此外,識別符號在 TypeScript 中可以具有多種型別。因此,使用 typeof 運算子,我們可以初始化識別符號後檢查其型別。
語法
下面的語法演示了 typeof 運算子與識別符號一起使用的示例。
let type = typeof <variable>
其中,
variable − 是需要檢查型別的識別符號。
返回值
它根據 typeof 運算子運算元的資料型別返回一個字串。
示例
讓我們透過 TypeScript 中的示例來了解 typeof 運算子 −
示例 1
在這個示例中,我們使用了 typeof 運算子與字串、數字、布林值和未定義變數一起使用。此外,我們還使用 typeof 運算子與值為 null 的變數一起使用。
在輸出中,我們可以看到它返回表示識別符號型別的字串。對於 null_value1 識別符號,typeof 運算子返回 object。
// Defining the variables of particular type and checking its type. let str1: string = "TutorialsPoint"; console.log(typeof str1); let num1: number = 32; console.log(typeof num1); let bool1: boolean = true; console.log(typeof bool1); let un_defined; console.log(typeof un_defined); let null_value1 = null; console.log(typeof null_value1);
編譯後,它將生成以下 JavaScript 程式碼 −
// Defining the variables of particular type and checking its type. var str1 = "TutorialsPoint"; console.log(typeof str1); var num1 = 32; console.log(typeof num1); var bool1 = true; console.log(typeof bool1); var un_defined; console.log(typeof un_defined); var null_value1 = null; console.log(typeof null_value1);
輸出
以上程式碼將產生以下輸出 −
string number boolean undefined object
示例 2
在下面的示例中,我們建立了一個包含一些鍵值對的物件,一個返回數字值的函式,以及一個包含不同值的數字陣列。
此外,我們還使用了 typeof 運算子來檢查所有型別的型別。typeof 運算子對於物件返回字串 "object",對於函式返回字串 "function",對於陣列返回字串 "object",因為陣列是物件的例項。
// using the typeof operator with object, function, and array. let demo_obj = { prop: "value", }; console.log(typeof demo_obj); function func() { let a = 10 + 20; return a; } console.log(typeof func); let array: Array<number> = [4, 23, 212, 2123, 323, 3]; console.log(typeof array);
編譯後,它將生成以下 JavaScript 程式碼 −
// using the typeof operator with object, function, and array. var demo_obj = { prop: "value" }; console.log(typeof demo_obj); function func() { var a = 10 + 20; return a; } console.log(typeof func); var array = [4, 23, 212, 2123, 323, 3]; console.log(typeof array);
輸出
以上程式碼將產生以下輸出 −
object function object
示例 3
在這個示例中,我們使用 typeof 運算子檢查了 NaN 關鍵字的型別,它返回字串 "number"。負無窮大的型別也是數字。PI 是 Math 類的屬性,它包含數字值。這就是為什麼 typeof 運算子對於 Math.PI 屬性返回 "number"。
pow() 是 Math 庫函式,用於獲取任何數字的冪。因此,typeof 運算子對於 Math.pow() 方法返回 "function"。
// using the typeof operator with NaN, infinity, library methods, and members. let NaN_type = NaN; console.log(typeof NaN_type); let infi = -Infinity; console.log(typeof infi); let pI = Math.PI; console.log(typeof pI); let pow = Math.pow; console.log(typeof pow);
編譯後,它將生成以下 JavaScript 程式碼 −
// using the typeof operator with NaN, infinity, library methods, and members. var NaN_type = NaN; console.log(typeof NaN_type); var infi = -Infinity; console.log(typeof infi); var pI = Math.PI; console.log(typeof pI); var pow = Math.pow; console.log(typeof pow);
輸出
以上程式碼將產生以下輸出 −
number number number function
示例 4
在這個示例中,我們使用 typeof 運算子獲取 variable1 的型別,並定義與 variable1 型別相同的 variable2。此外,我們還使用類型別名來儲存 variable3 的型別。名為 type1 的型別包含 variable3 的型別,它是一個字串。
之後,我們使用 type1 來定義 variable4 的型別。在輸出中,使用者可以觀察到 variable4 的型別與 variable3 相同。
// defining the variable1 of type number var variable1: number = 10; // To define the variable2 as of same type variable1 let variable2: typeof variable1 = 20; console.log(typeof variable2); // variable3 of string type var variable3: string = "Hello!"; // using the type alias to define the type of same type as variable3 type type1 = typeof variable3; // defining the variable4 of type named type1 let variable4: type1 = "Hi"; console.log(typeof variable4);
編譯後,它將生成以下 JavaScript 程式碼 −
// defining the variable1 of type number var variable1 = 10; // To define the variable2 as of same type variable1 var variable2 = 20; console.log(typeof variable2); // variable3 of string type var variable3 = "Hello!"; // defining the variable4 of type named type1 var variable4 = "Hi"; console.log(typeof variable4);
輸出
以上程式碼將產生以下輸出 −
number string