WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

TypeScript · Advanced · question 49 of 100

Can you give an example of using a ’symbol’ type in TypeScript?

📕 Buy this interview preparation book: 100 TypeScript questions & answers — PDF + EPUB for $5

In TypeScript, the ‘symbol‘ type is used to create unique identifiers. Symbols can be especially useful as object keys to avoid name collisions when using third-party libraries.

Consider the following example:

// Create a unique symbol identifier:
const uniqueSymbol = Symbol("exampleSymbol");

// Use the symbol as a property key in an object:
const obj = {
  [uniqueSymbol]: "This is a unique value",
  someStringKey: "This is a regular string key value"
};

// Get the value associated with the symbol key:
console.log(obj[uniqueSymbol]); // Output: "This is a unique value"

In this example, we create a unique symbol identifier called ‘uniqueSymbol‘. A description "exampleSymbol" is provided to the ‘Symbol()‘ function, which is only used for debugging purposes and does not impact the uniqueness of the symbol.

Next, an object called ‘obj‘ is created with two properties. The first property uses the unique symbol as its key, and the second property is a regular string key (‘someStringKey‘).

Finally, we access and log the value associated with the ‘uniqueSymbol‘ key in the ‘obj‘.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic TypeScript interview — then scores it.
📞 Practice TypeScript — free 15 min
📕 Buy this interview preparation book: 100 TypeScript questions & answers — PDF + EPUB for $5

All 100 TypeScript questions · All topics