如何在 TypeScript 中使用 typeof 操作符

如何在 TypeScript 中使用 typeof 操作符

在TypeScript中,typeof是检查变量或标识符类型的最有用的操作符和关键字之一。然而,TypeScript是一种类型严格的语言。所以,我们需要在定义标识符本身的同时定义变量或对象的类型。然而,有时我们还是需要检查变量的类型。

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

语法

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

let type = typeof <variable>
TypeScript

参数

  • variable- 它是一个标识符,我们需要检查它的类型。

返回值

它根据typeof操作符的操作数的数据类型返回一个字符串。

示例 1

在这个例子中,我们对字符串、数字、布尔值和未定义变量使用了typeof运算符。此外,我们还对含有null值的变量使用了typeof运算符。

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

// 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);
TypeScript
// 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);
TypeScript

输出

上述代码将产生以下输出 —

string
number
boolean
undefined
object
TypeScript

示例 2

在下面的例子中,我们已经创建了包含一些键值对的异议,一个返回数字值的函数,以及一个包含不同数值的数字数组。

另外,我们用typeof运算符来检查所有的类型。typeof运算符为对象返回 “object “字符串,为Function返回 “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);
TypeScript
// 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);
TypeScript

输出

上述代码将产生以下输出 —

object
function
object
TypeScript

示例 3

在这个例子中,我们用typeof运算符检查了NaN关键字的类型,它返回的是 “数字 “字符串。负数Infinity的类型也是一个数字。PI是数学类的属性,它包含数字值。这就是为什么typeof运算符为Math.PI属性返回 “数字”。

pow()是数学库中的函数,用于获取任何数字的幂。因此,typeof操作符返回Math.pow()方法的 “函数”。

// 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);
TypeScript
// 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);
TypeScript

输出

上述代码将产生以下输出 —

number
number
number
function
TypeScript

示例 4

在这个例子中,我们使用typeof操作符来获取变量1的类型,并将变量2定义为与变量1相同的类型。另外,我们使用类型别名来存储变量3的类型。命名为type1的类型包含了变量3的类型,它是一个字符串。

之后,我们用type1来定义变量4的类型。在输出中,用户可以观察到,变量4的类型与变量3相同。

// 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);
TypeScript
// 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);
TypeScript

输出

上述代码将产生以下输出 —

number
string
TypeScript

我们在本教程中学习了TypeScript中typeof运算符的使用。通过各种例子,我们已经看到typeof运算符是如何与不同的数据类型一起工作的。如果我们使用构造函数来创建数字、布尔或字符串变量,那么typeof运算符返回 “对象 “作为其类型。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册