检查 TypeScript 中变量的类型

在 TypeScript 中检查变量的类型

Check the Type of a Variable in TypeScript

使用typeof运算符检查 TypeScript 中变量的类型,例如
if (typeof myVar === 'string') {}. typeof运算符返回一个字符串,指示值的类型,可用作 TypeScript 中的类型保护。

索引.ts
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 ,可用作
类型保护

索引.ts
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 存储stringif块中。

变量可能存储的唯一其他可能类型是 a number,因此变量numberelse块中的类型为 a 。

下面是一些使用typeof运算符的示例。

索引.ts
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()方法。

索引.ts
const arr: string[] = ['a', 'b', 'c']; console.log(Array.isArray(arr)); // 👉️ true
Array.isArray方法返回一个布尔结果——如果传入的值是一个数组,否则。 truefalse

NaN(不是数字)的类型是number如果您需要检查特定值是否为NaN,请使用Number.isNaN方法。

索引.ts
const example = Number('hello'); console.log(example); // 👉️ NaN if (Number.isNaN(example)) { console.log('Passed in value is NaN'); }

如果传入的值的类型为并且 是 ,则Number.isNaN方法将返回truenumberNaN

您这样做时,typeof操作员会返回"object"typeof null

索引.ts
console.log(typeof null); // 👉️ "object"

如果要检查变量是否存储null值,请不要使用
typeof运算符,而是直接检查。

索引.ts
const example = null; if (example === null) { console.log('Variable stores a null value'); }

请注意,typeof运算符始终返回一个字符串。一个很常见的错误是做类似下面的事情。

索引.ts
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
运算符。

索引.ts
class Person {} const person = new Person(); if (person instanceof Person) { console.log('value is an instance of Person'); }

instanceof运算符返回一个布尔值,指示构造函数的原型属性是否出现在对象的原型链中。

person对象是使用Person该类创建的,因此它是该类的一个实例。

instanceof运算符在 TypeScript 中非常有用,因为它可以用作
类型保护

索引.ts
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(); } }

该函数采用Personor类型的参数Animal,因此在我们访问特定于类的方法之前,我们必须检查传递给该函数的类的实例。

发表评论