- TypeScript 基础
- TypeScript - 首页
- TypeScript - 路线图
- TypeScript - 概述
- TypeScript - 环境设置
- TypeScript - 基本语法
- TypeScript vs. JavaScript
- TypeScript - 特性
- TypeScript - 变量
- TypeScript - let & const
- TypeScript - 运算符
- TypeScript 基本类型
- TypeScript - 类型
- TypeScript - 类型注解
- TypeScript - 类型推断
- TypeScript - 数字
- TypeScript - 字符串
- TypeScript - 布尔值
- TypeScript - 数组
- TypeScript - 元组
- TypeScript - 枚举
- TypeScript - Any
- TypeScript - Never
- TypeScript - 联合类型
- TypeScript - 字面量类型
- TypeScript - 符号
- TypeScript - null vs. undefined
- TypeScript - 类型别名
- TypeScript 控制流
- TypeScript - 决策
- TypeScript - if 语句
- TypeScript - if else 语句
- TypeScript - 嵌套 if 语句
- TypeScript - switch 语句
- TypeScript - 循环
- TypeScript - for 循环
- TypeScript - while 循环
- TypeScript - do while 循环
- TypeScript 函数
- TypeScript - 函数
- TypeScript - 函数类型
- TypeScript - 可选参数
- TypeScript - 默认参数
- TypeScript - 匿名函数
- TypeScript - 函数构造器
- TypeScript - rest 参数
- TypeScript - 参数解构
- TypeScript - 箭头函数
- TypeScript 接口
- TypeScript - 接口
- TypeScript - 扩展接口
- TypeScript 类和对象
- TypeScript - 类
- TypeScript - 对象
- TypeScript - 访问修饰符
- TypeScript - 只读属性
- TypeScript - 继承
- TypeScript - 静态方法和属性
- TypeScript - 抽象类
- TypeScript - 访问器
- TypeScript - 鸭子类型
- TypeScript 高级类型
- TypeScript - 交叉类型
- TypeScript - 类型守卫
- TypeScript - 类型断言
- TypeScript 类型操作
- TypeScript - 从类型创建类型
- TypeScript - Keyof 类型操作符
- TypeScript - Typeof 类型操作符
- TypeScript - 索引访问类型
- TypeScript - 条件类型
- TypeScript - 映射类型
- TypeScript - 模板字面量类型
- TypeScript 泛型
- TypeScript - 泛型
- TypeScript - 泛型约束
- TypeScript - 泛型接口
- TypeScript - 泛型类
- TypeScript 其他
- TypeScript - 三斜杠指令
- TypeScript - 命名空间
- TypeScript - 模块
- TypeScript - 环境声明
- TypeScript - 装饰器
- TypeScript - 类型兼容性
- TypeScript - Date 对象
- TypeScript - 迭代器和生成器
- TypeScript - Mixins
- TypeScript - 实用程序类型
- TypeScript - 装箱和拆箱
- TypeScript - tsconfig.json
- 从 JavaScript 到 TypeScript
- TypeScript 有用资源
- TypeScript - 快速指南
- TypeScript - 有用资源
- TypeScript - 讨论
TypeScript - 符号
在 TypeScript 中,符号是一种原始数据类型,它是唯一且不可变的。符号是在 ECMAScript 2015(也称为 ES6)中引入的。
就像我们使用数字、字符串或布尔值来创建不同数据类型的变量一样,我们可以使用符号类型来创建符号。
使用符号类型有很多好处,因为它提供了比其他数据类型更多的功能。在本教程中,我们将学习符号的基础知识及其不同用途。
语法
TypeScript 中的符号是使用 Symbol() 构造函数创建的 -
let test_symbol = Symbol();
您可以将键作为符号参数传递以标识符号。
let key_symbol = Symbol("key-for-symbol");
符号是唯一且不可变的
在 TypeScript 中,您可以创建多个唯一的符号。即使您使用相同的键创建符号,它们也将是唯一的。
让我们创建两个具有相同键的不同符号。
let first_sym = Symbol("sym");
let second_sym = Symbol("sym");
console.log(first_sym === second_sym);
以上 TypeScript 代码显示了这两个符号是唯一的并且不可比较的。
编译后,它将在 JavaScript 中生成相同的代码。
输出如下 -
false
符号作为对象属性的键
符号也可以像字符串一样用作对象属性的键。
在下面的示例中,我们创建了符号并定义了对象。我们使用 obj_symbol 作为对象的属性。此外,我们可以像访问对象的普通属性一样访问它。
const obj_symbol = Symbol();
// creating the object
let object = {
// using the obj_symbol as key of object
[obj_symbol]: "obj value",
};
// accessing the symbol property of the object
console.log(
"The value of the obj_symbol property in the object is " + object[obj_symbol]
);
编译后,它将在 JavaScript 中生成相同的代码。
它将产生以下输出 -
The value of the obj_symbol property in the object is obj value
带 switch case 语句的符号
由于每个符号都是唯一的,因此我们可以将其用作 switch-case 语句的 case。当我们使用符号与 switch case 语句时,它确保每个 case 都是唯一的。如果任何 case 与作为 switch 语句参数传递的 case 不匹配,则它将转到 default case。
switch(symbol) {
case symbol1:
break
case symbol2:
break;
}
在上面的语法中,符号作为 switch 语句的参数传递。之后,我们使用符号名称后跟 case 关键字来创建不同的 case。
示例
在下面的示例中,我们创建了四个不同的符号。之后,我们定义了 print_symbol 函数,其中包含 switch case 语句来处理不同的 case。
在 switch case 语句中,我们使用符号值作为 case 并执行特定 case 的代码。
// different symbols
const symbol1 = Symbol();
const symbol2 = Symbol();
const symbol3 = Symbol();
const symbol4 = Symbol();
function print_symbol(symbol) {
// creating the switch case statement
switch (symbol) {
// different cases
case symbol1:
console.log("The case is symbol 1.");
break;
case symbol2:
console.log("The case is symbol 2.");
break;
case symbol3:
console.log("The case is symbol 3.");
debugger;
break;
case symbol4:
console.log("The case is symbol 4.");
break;
default:
console.log("The case is default.");
}
}
// calling the function
print_symbol(symbol2);
print_symbol(symbol4);
编译后,它将在 JavaScript 中生成相同的代码。
它将产生以下输出 -
The case is symbol 2. The case is symbol 4.
唯一符号
在 TypeScript 中,Symbol 是一种原始数据类型。因此,我们只需要将其用作类型,但我们也可以使用“唯一符号”将其用作字面量。符号包含唯一符号,这意味着唯一符号是符号的子类型。
我们只能将唯一符号与 const 变量和只读属性一起使用。如果我们想将特定符号类型引用到另一个变量,我们可以使用“typeof”运算符。
语法
您可以按照以下语法使用符号作为使用唯一符号的字面量。
const test_symbol: unique symbol = Symbol();
let symbol1: typeof test_symbol = test_symbol;
class C {
static readonly symb: unique symbol = Symbol("unique symbol");
}
示例
在下面的示例中,我们声明了类型为 symbol 的 test_symbol,并使用 unique symbol 关键字将符号用作类型字面量。此外,用户可以观察我们如何使用 typeof 运算符将符号用作使用 let 和 var 关键字声明的变量的类型字面量。
此外,我们使用了 unique symbol 关键字来定义只读静态类的成员的类型。
// here, we used the unique symbol to define the type of the sym1.
const test_symbol: unique symbol = Symbol();
// we can't reference the unique symbol to the let type of variable
// let sym2: unique symbol = Symbol();
// to reference the symbol type to the variables declared with the let keyword, using the typeof operator.
let symbol1: typeof test_symbol = test_symbol;
console.log("The value of symbol1 is " + typeof test_symbol);
// referencing the unique symbol to the static class property
class C {
static readonly symb: unique symbol = Symbol("unique symbol");
}
编译后,它将生成以下 JavaScript 代码 -
// here, we used the unique symbol to define the type of the sym1.
var test_symbol = Symbol();
// we can't reference the unique symbol to the let type of variable
// let sym2: unique symbol = Symbol();
// to reference the symbol type to the variables declared with the let keyword, using the typeof operator.
var symbol1 = test_symbol;
console.log("The value of symbol1 is " + typeof test_symbol);
// referencing the unique symbol to the static class property
var C = /** @class */ (function () {
function C() {
}
C.symb = Symbol("unique symbol");
return C;
}());
以上代码将产生以下输出 -
The value of symbol1 is symbol