Working with TypeScript Symbols - Unleash the Power of Symbols in TypeScript

24 Jun 2023 Balmiki Mandal 0 Typescript

Working with TypeScript Symbols

TypeScript symbols are a special kind of data type that can be used to store and reference unique values. Symbols can be used in various situations, such as creating constants, guarding property keys, and assigning unique ids. In this article, we'll explore the different ways you can use symbols in TypeScript.

Creating Constants with Symbols

One of the most common uses for symbols is to create constant values. Since the value of a symbol is immutable, it can be used to store unique values that can't be changed. For example, imagine that you have an object with several options that you want to make available to users. You could use symbols to create the option values.

const Option1 = Symbol();
const Option2 = Symbol();

const options = {
    [Option1]: 'Option 1',
    [Option2]: 'Option 2'
};
console.log(options[Option1]); // 'Option 1'

In this example, we create two symbols that can be used to store values, Option1 and Option2. We then use the symbols as keys in an object to store two strings.

Guarding Properties with Symbols

Another common use case for symbols is for guarding object properties. By using symbols as property keys, you can make sure that the properties can only be accessed by code that knows the symbol used to guard them. For example, imagine that we have an object with some properties that you only want certain parts of your code to be able to access.

const guardedProperty = Symbol();

const obj = {
    [guardedProperty]: 'secret value'
};

obj[guardedProperty]; // 'secret value'

In this example, we create a symbol and use it as the key to a property in an object. We then use that symbol to access the property. Because the property key is a symbol, only code that knows the symbol can access it.

Assigning Unique IDs with Symbols

Another useful application of symbols is for assigning unique IDs. By using symbols, you can generate identifiers for objects, functions, classes, etc. that can be used to refer to them programmatically. For example, imagine that you have a function that you want to assign an id to so that you can later refer to it.

const funcId = Symbol();

function exampleFunc() {
    // ...
}

exampleFunc[funcId] = 'unique-id';

console.log(exampleFunc[funcId]); // 'unique-id'

In this example, we create a symbol and use it to assign a unique ID to the exampleFunc function. This unique ID can be used to refer to the function, providing a simple way of identifying it in your code.

Conclusion

In this article, we looked at some of the different ways you can use symbols in TypeScript. Using symbols can be a great way to store unique values and to prevent accidentally changing or overwriting them. You can also use symbols to guard object properties and to assign unique IDs. With a bit of imagination, there are many more ways in which symbols can be used in TypeScript.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.