typescript 中检查变量的类型-ag捕鱼王app官网

typescript 中检查变量的类型

作者:迹忆客 最近更新:2022/11/07 浏览次数:

使用 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

typescript 中检查变量的类型

我们使用 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"

typescript typeof 一些示例

请注意 ,将 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');
}

typescript instanceof 运算符

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();
  }
}

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

上一篇:

下一篇:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 typescript 中返回一个 promise

发布时间:2023/03/19 浏览次数:586 分类:typescript

本教程讨论如何在 typescript 中返回正确的 promise。这将提供 typescript 中 returns promise 的完整编码示例,并完整演示每个步骤。

在 typescript 中定义函数回调的类型

发布时间:2023/03/19 浏览次数:1445 分类:typescript

本教程说明了在 typescript 中为函数回调定义类型的ag捕鱼王app官网的解决方案。为了程序员的方便和方便,实施了不同的编码实践指南。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图