在 TypeScript 中检查变量的类型
Check the Type of a Variable in TypeScript
使用typeof
运算符检查 TypeScript 中变量的类型,例如
if (typeof myVar === 'string') {}
. 该typeof
运算符返回一个字符串,指示值的类型,可用作 TypeScript 中的类型保护。
const myVar: string | number = 'hello world'; console.log(typeof myVar); // 👉️ "string" if (typeof myVar === 'string') { console.log(myVar.toUpperCase()); // 👉️ "HELLO WORLD" } // ✅ Use instanceof for classes class Person {} const person = new Person(); console.log(person instanceof Person); // 👉️ true const err = new Error('Something went wrong'); console.log(err instanceof Error); // 👉️ true // ✅ Use Array.isArray to check if array console.log(Array.isArray([1, 2, 3])); // 👉️ true
我们使用
typeof
运算符来检查 TypeScript 中变量的类型。
该运算符返回一个string
指示值类型的 a ,可用作
类型保护。
const myVar: string | number = Math.random() > 0.5 ? 'Hello' : 1000; // 👉️ myVar has type string or number here if (typeof myVar === 'string') { // 👇️ myVar has type string here console.log(myVar.toUpperCase()); } else { // 👇️ myVar has type number here console.log(myVar.toFixed(2)); }
该myVar
变量是使用
联合
类型的,并且可以具有字符串或数字类型。
我们不能直接访问变量上的字符串内置方法,因为它可能是一个数字。
if
条件中,我们检查的类型myVar
是否为字符串,因此 TypeScript 知道变量将 a 存储string
在if
块中。变量可能存储的唯一其他可能类型是 a number
,因此变量number
在else
块中的类型为 a 。
下面是一些使用typeof
运算符的示例。
console.log(typeof 'hello'); // 👉️ "string" console.log(typeof 100); // 👉️ "number" console.log(typeof true); // 👉️ "boolean" console.log(typeof [1, 2, 3]); // 👉️ "object" console.log(typeof {}); // 👉️ "object" console.log(typeof function example() {}); // 👉️ "function" console.log(typeof NaN); // 👉️ "number" console.log(typeof undefined); // 👉️ "undefined" console.log(typeof null); // 👉️ "object" console.log(typeof class A {}); // 👉️ "function"
请注意,将typeof
运算符与数组一起使用会返回object
.
要检查变量是否存储在数组中,请使用该Array.isArray()
方法。
const arr: string[] = ['a', 'b', 'c']; console.log(Array.isArray(arr)); // 👉️ true
Array.isArray
方法返回一个布尔结果——如果传入的值是一个数组,否则。 true
false
NaN
(不是数字)的类型是number
。如果您需要检查特定值是否为NaN
,请使用Number.isNaN
方法。
const example = Number('hello'); console.log(example); // 👉️ NaN if (Number.isNaN(example)) { console.log('Passed in value is NaN'); }
如果传入的值的类型为并且 是 ,则该Number.isNaN
方法将返回。true
number
NaN
当您这样做时,typeof
操作员会返回。"object"
typeof null
console.log(typeof null); // 👉️ "object"
如果要检查变量是否存储null
值,请不要使用
typeof
运算符,而是直接检查。
const example = null; if (example === null) { console.log('Variable stores a null value'); }
请注意,typeof
运算符始终返回一个字符串。一个很常见的错误是做类似下面的事情。
const myVar = undefined; if (typeof myVar === undefined) { console.log('myVar is undefined'); } else { console.log('👉️ This block runs'); }
运行示例中的else
块是因为我们正在检查的类型
myVar
是否等于值undefined
。这计算为false
因为 typeofmyVar
返回一个字符串"undefined"
,而不是值undefined
。
typeof
运算符返回类"function"
。这是因为 JavaScript(和 TypeScript)中的类只是函数的语法糖。如果需要检查变量是否存储特定类的实例,请使用
instanceof
运算符。
class Person {} const person = new Person(); if (person instanceof Person) { console.log('value is an instance of Person'); }
该instanceof
运算符返回一个布尔值,指示构造函数的原型属性是否出现在对象的原型链中。
该person
对象是使用Person
该类创建的,因此它是该类的一个实例。
该instanceof
运算符在 TypeScript 中非常有用,因为它可以用作
类型保护。
class Person { walk() { console.log('person is walking'); } } class Animal { run() { console.log('animal is running'); } } function example(x: Person | Animal) { // 👉️ x is type Person or Animal here if (x instanceof Person) { // 👇️ x is type Person here x.walk(); } else { // 👇️ x is type Animal here x.run(); } }
该函数采用Person
or类型的参数Animal
,因此在我们访问特定于类的方法之前,我们必须检查传递给该函数的类的实例。