TypeScript 中 typeof 类型操作符怎么用?

文章导读
Previous Quiz Next 在 TypeScript 中,typeof 是检查变量或标识符类型的最有用的运算符和关键字之一。然而,TypeScript 是一种类型严格的语言。因此,我们需要在定义标识符本身时定义变量或对象的类型。尽管如此,有时我们仍需要检查变量的类
📋 目录
  1. 语法
  2. 示例
A A

TypeScript - Typeof 类型运算符



Previous
Quiz
Next

在 TypeScript 中,typeof 是检查变量或标识符类型的最有用的运算符和关键字之一。然而,TypeScript 是一种类型严格的语言。因此,我们需要在定义标识符本身时定义变量或对象的类型。尽管如此,有时我们仍需要检查变量的类型。

例如,我们通过表单从用户获取数据,并使用 TypeScript 处理它。然后我们必须验证数据并执行一些数据库操作。此外,在 TypeScript 中,标识符可以具有多种类型。因此,使用 typeof 运算符,我们可以在初始化后检查标识符的类型。

语法

下面的语法演示了 typeof 运算符与标识符的使用。

let type = typeof <variable>

其中,

  • variable 是我们需要检查类型的标识符。

返回值

它根据 typeof 运算符的操作数的 data type 返回一个字符串。

示例

让我们通过 TypeScript 中的示例来理解 typeof 操作符 −

示例 1

在这个示例中,我们使用了 typeof 操作符来检查 string、number、boolean 和 undefined 变量。另外,我们还对包含 null 值的变量使用了 typeof 操作符。

在输出中,我们可以看到它返回表示标识符类型的字符串。对于 null_value1 标识符,typeof 操作符返回 object。

// 定义特定类型的变量并检查其类型。
let str1: string = "";
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 = "";
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

在下面的示例中,我们创建了一个包含一些键值对的对象、一个返回 number 值的 function,以及一个包含不同值的 numbers 数组。

另外,我们使用 typeof 操作符检查了它们的所有类型。typeof 操作符对于 object 返回 "object" 字符串,对于 Function 返回 "function" 字符串,对于 array 返回 "object" 字符串,因为 array 是 object 的实例。

// 使用 typeof 操作符检查 object、function 和 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" 字符串。负无穷大的类型也是 number。PI 是 Math class 的属性,包含 number 值。因此,typeof 操作符对于 Math.PI 属性返回 "number"。

pow() 是 Math 库函数,用于获取任意数字的幂。因此,typeof 操作符对于 Math.pow() 方法返回 "function"。

// 使用 typeof 操作符检查 NaN、无穷大、库方法和成员。
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。另外,我们使用 type alias 来存储 variable3 的类型。名为 type1 的类型包含 variable3 的类型,即 string。

之后,我们使用 type1 来定义 variable4 的类型。在输出中,用户可以看到 variable4 的类型与 variable3 相同。

// 定义类型为 number 的 variable1
var variable1: number = 10;

// 定义与 variable1 相同类型的 variable2
let variable2: typeof variable1 = 20;
console.log(typeof variable2);

// string 类型的 variable3
var variable3: string = "Hello!";

// 使用 type alias 定义与 variable3 相同类型的类型
type type1 = typeof variable3;

// 定义类型为 type1 的 variable4
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