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
广告

© . All rights reserved.