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
廣告