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 中变量的类型。
运算符返回一个字符串,指示值的类型,可以用作类型保护。
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 知道该变量在 if 块中存储了一个字符串。
变量可能存储的唯一其他可能类型是数字,因此变量在 else 块中被键入为数字。
以下是使用 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 运算符与数组一起使用会返回对象。
要检查变量是否存储在数组中,请使用 array.isarray()
方法。
const arr: string[] = ['a', 'b', 'c'];
console.log(array.isarray(arr)); // 👉️ true
array.isarray
方法返回一个布尔结果 - 如果传入的值是一个数组,则返回 true,否则返回 false。
nan(不是数字)的类型是数字。 如果我们需要检查特定值是否为 nan,请使用 number.isnan
方法。
const example = number('hello');
console.log(example); // 👉️ nan
if (number.isnan(example)) {
console.log('passed in value is nan');
}
如果传入的值的类型为数字并且为 nan,则 number.isnan
方法将返回 true。
当我们执行 typeof null
时,typeof 运算符返回“object”。
console.log(typeof null); // 👉️ "object"
如果要检查变量是否存储空值,请不要使用 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 的类型是否等于未定义的值。 这计算结果为 false,因为 typeof myvar 返回一个“未定义”字符串,而不是未定义的值。
typeof
运算符为类返回“函数”。 这是因为 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
或 animal
类型的参数,因此在访问特定于类的方法之前,我们必须检查传递给函数的类的实例。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在 angularjs 中设置 select from typescript 的默认选项值
发布时间:2023/04/14 浏览次数:132 分类:angular
-
本教程提供了在 angularjs 中从 typescript 中设置 html 标记选择的默认选项的解释性ag捕鱼王app官网的解决方案。
在 angular 中使用 typescript 的 getelementbyid 替换
发布时间:2023/04/14 浏览次数:259 分类:angular
-
本教程指南提供了有关使用 typescript 在 angular 中替换 document.getelementbyid 的简要说明。这也提供了在 angular 中 getelementbyid 的最佳方法。
在 typescript 中使用 try..catch..finally 处理异常
发布时间:2023/03/19 浏览次数:385 分类:typescript
-
本文详细介绍了如何在 typescript 中使用 try..catch..finally 进行异常处理,并附有示例。
在 typescript 中使用 declare 关键字
发布时间:2023/03/19 浏览次数:254 分类:typescript
-
本教程指南通过特定的实现和编码示例深入了解了 typescript 中 declare 关键字的用途。
在 typescript 中 get 和 set
发布时间:2023/03/19 浏览次数:962 分类:typescript
-
本篇文章演示了类的 get 和 set 属性以及如何在 typescript 中实现它。
在 typescript 中格式化日期和时间
发布时间:2023/03/19 浏览次数:269 分类:typescript
-
本教程介绍内置对象 date() 并讨论在 typescript 中获取、设置和格式化日期和时间的各种方法。
在 typescript 中返回一个 promise
发布时间:2023/03/19 浏览次数:586 分类:typescript
-
本教程讨论如何在 typescript 中返回正确的 promise。这将提供 typescript 中 returns promise 的完整编码示例,并完整演示每个步骤。
在 typescript 中定义函数回调的类型
发布时间:2023/03/19 浏览次数:1445 分类:typescript
-
本教程说明了在 typescript 中为函数回调定义类型的ag捕鱼王app官网的解决方案。为了程序员的方便和方便,实施了不同的编码实践指南。
在 typescript 中把 json 对象转换为一个类
发布时间:2023/03/19 浏览次数:521 分类:typescript
-
本教程演示了如何将 json 对象转换为 typescript 中的类。