TypeScript 类型转换

注意:如果您是TypeScript新手,请先查看我们的TypeScript入门指南


在编程中,类型转换是将一种数据的类型转换为另一种类型的过程。例如,将字符串数据转换为数字。

TypeScript中有两种类型的类型转换

  • 隐式转换 - 自动类型转换。
  • 显式转换 - 手动类型转换。

注意:TypeScript不会改变JavaScript引擎处理转换的方式。它只确保您有意识地执行转换并及早发现错误。


TypeScript隐式转换

在某些情况下,JavaScript在执行过程中会自动转换数据类型。由于TypeScript编译为JavaScript,因此它保留了这种行为——这被称为隐式类型转换。例如,

let result: any;

// Convert number to string
result = "3" + 2;
console.log(result, "-", typeof result);

// Convert boolean to string
result = "3" + true;
console.log(result, "-", typeof result);

// Convert null to string
result = "3" + null;
console.log(result, "-", typeof result);

输出

32 - string
3true - string
3null - string

在此示例中,我们使用+运算符将字符串与其他数据类型进行隐式类型转换。

这里,

  • "3" + 2 - 将数字2转换为字符串,并将其连接到"3",结果为字符串"32"
  • "3" + true - 将布尔值true转换为字符串,并将其连接到"3",结果为字符串"3true"
  • "3" + null - 将null转换为字符串,并将其连接到"3",结果为字符串"3null"

注意typeof运算符会给出变量的数据类型。


TypeScript显式转换

在显式类型转换中,您可以使用JavaScript的内置函数手动将一种数据类型转换为另一种数据类型。例如,

let result: any;

// Convert string to number
result = Number("5");
console.log(result, "-", typeof result);

// Convert boolean to string
result = String(true);
console.log(result, "-", typeof result);

// Convert number to boolean
result = Boolean(0);
console.log(result, "-", typeof result);

输出

5 - number
true - string
false - boolean

这里,

  • Number("5") - 将字符串"5"更改为数字5
  • String(true) - 将布尔值true转换为字符串"true"
  • Boolean(0) - 返回false,因为0是JavaScript中的falsy值。由于TypeScript编译为JavaScript,因此这种行为在运行时会得到保留。

更多关于TypeScript类型转换

转换为数字

当您需要在TypeScript中对数字字符串执行算术运算时,最好使用Number()函数显式转换它们。

这是因为即使JavaScript允许运行时隐式强制类型转换,TypeScript也可能会显示类型错误。

让我们看一个例子。

// Numeric string used with - , / , *
// results in number type

let result: number;

result = Number("4") - Number("2");
console.log(result, "-", typeof result);

result = Number("4") - 2;
console.log(result, "-", typeof result);

result = Number("4") * 2;
console.log(result, "-", typeof result);

result = Number("4") / 2;
console.log(result, "-", typeof result);

输出

2 - number
2 - number
8 - number
2 - number
TypeScript类型转换表

该表显示了TypeScript中各种值到StringNumberBoolean的转换。

字符串转换 数字转换 布尔值转换
1 "1" 1 true
0 "0" 0 false
"1" "1" 1 true
"0" "0" 0 true
"ten" "ten" NaN true
true "true" 1 true
false "false" 0 false
null "null" 0 false
undefined "undefined" NaN false
'' "" 0 false
' ' " " 0 true
类型转换规则

理解类型转换的规则对于避免TypeScript代码中出现意外结果至关重要。

以下是一些要点:

  1. 涉及二元+的运算在字符串和数字连接时会将数字转换为字符串。
  2. 逻辑运算将操作数转换为布尔值。
  3. -*/等运算符会在运行时将字符串转换为数字,但TypeScript可能需要显式转换以确保类型安全。
  4. 将复杂类型(如对象)转换为原始类型可能会导致数据丢失或意外的字符串输出。
TypeScript 类型断言

TypeScript本身不允许在运行时进行类型转换。所有运行时类型转换/强制类型转换都是由JavaScript执行的。

TypeScript除了JavaScript还提供类型断言,您可以手动告知TypeScript值的类型。

类型断言在运行时不会更改值;它只告诉TypeScript在编译时将该值视为特定类型。例如,

let value: any = "hello";
let strLength = (value as string).length;

console.log(strLength);

// Output: 5

在这里,我们告诉TypeScript将value视为字符串,以便我们可以安全地访问字符串的length属性。

要了解更多信息,请访问TypeScript类型断言


另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战