TypeScript - Symbols



In TypeScript, a symbol is a primitive data type that is unique and immutable. The symbols are introduced in ECMAScript 2015 (also known as ES6).

As we use the number, string, or boolean to create the variables of different data-type, we can use the symbol type to create the Symbol.

Using the symbol types has many benefits as it provides more features than other data types. In this tutorial, we will learn about the basics of the Symbol and its different uses of the Symbol.

Syntax

Symbols in TypeScript are created using the Symbol() constructor function –

let test_symbol = Symbol();

You can pass a key as a symbol parameter to identify the Symbol.

let key_symbol = Symbol("key-for-symbol"); 

Symbols are unique and immutable

In TypeScript, you can create multiple symbols that are unique. Even if you create the symbols with the same keys, they will be unique.

Let's create two different symbols with the same key.

let first_sym = Symbol("sym");
let second_sym = Symbol("sym");
console.log(first_sym === second_sym);

The above TypeScript code show that both symbols are unique and not comparable.

On compiling, it will generate the same code in JavaScript.

The output is as follows –

false

Symbols as keys for object properties

Symbols can also be used as keys for object properties just like strings.

In the example below, we have created the symbol and defined the object. We used the obj_symbol as a property of the object. Also, we can access it like the object's normal property.

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]
);

On compiling, it will generate the same code in JavaScript.

It will produce the following output −

The value of the obj_symbol property in the object is obj value

Symbol with the switch case statement

As every symbol is unique, we can use it as a case of the switch-case statement. When we use the symbols with the switch case statement, it ensures that every case is unique. If any case doesn’t match with the case passed as a parameter of the switch statement, it goes to the default case.

switch(symbol) {
   case symbol1: 
      break
   case symbol2: 
      break;
}

In the above syntax, a symbol is passed as a parameter of the switch statement. After that, we used the symbol name followed by the case keyword to create a different case.

Example

In the example below, we have created four different symbols. After that, we defined the print_symbol function, which contains the switch case statement to handle the different cases.

In the switch case statement, we are using the symbol values as a case and executing the code of the particular 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);

On compiling, it will generate the same code in JavaScript.

It will produce the following output −

The case is symbol 2.
The case is symbol 4.

Unique Symbols

In TypeScript, Symbol is a primitive data type. So, we need to use it as a type only, but we can also use it as literals using the ‘unique symbol’. The symbol includes the unique symbol, which means the unique symbol is a subtype of the symbol.

We can use the unique symbol with only const variables and readonly properties. If we want to reference the specific symbol type to another variable, we can use the ‘typeof’ operator.

Syntax

You can follow the syntax below to use the symbol as a literal using the unique symbol.

const test_symbol: unique symbol = Symbol();
let symbol1: typeof test_symbol = test_symbol;
class C {
   static readonly symb: unique symbol = Symbol("unique symbol");
}

Example

In the example below, we have declared the test_symbol of the type symbol and used the unique symbol keyword to use the symbol as a type literal. Also, users can observe how we can use the typeof operator to use the symbol as a type literal of the variables declared with the let and var keywords.

Also, we have used the unique symbol keyword to define the type of the readonly static class’s member.

// 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");
}

On compiling, it will generate the following JavaScript code −

// 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 above code will produce the following output −

The value of symbol1 is symbol
Advertisements