Symbols Flashcards
What is a symbol in TypeScript and how can you create one?
In TypeScript, a symbol is a primitive data type, similar to number and string, introduced in ECMAScript 2015. Symbols are created by calling the Symbol
constructor, and they are immutable and unique. They can optionally have a string key.
Here’s an example:
let sym1 = Symbol(); let sym2 = Symbol("key"); let sym3 = Symbold("key"); sym2 === sym3 // false, symbols are unique
In this example, sym1
and sym2
are symbols. Even though sym2
and sym3
have the same key, they are not the same symbol.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
What is the purpose of Symbol.hasInstance
?
Symbol.hasInstance
represents a method that determines if a constructor object recognizes an object as one of the constructor’s instances. It is called by the semantics of the instanceof
operator. It allows you to customize the behavior of the instanceof
operator.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
What is the purpose of Symbol.asyncIterator
?
Symbol.asyncIterator
represents a method that returns an async
iterator for an object, which can be used with a for await..of
loop. This symbol is used to make an object iterable asynchronously.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
What is a unique symbol in TypeScript and how does it work?
A unique symbol in TypeScript is a subtype of symbol
that enables treating symbols as unique literals.
To enable treating symbols as unique literals a special type unique symbol
is available. unique symbols are produced only from calling Symbol()
or Symbol.for()
, or from explicit type annotations. This type is only allowed on const
declarations and readonly static
properties, and in order to reference a specific unique symbol, you’ll have to use the typeof
operator. Each reference to a unique symbol implies a completely unique identity that’s tied to a given declaration.
declare const sym1: unique symbol; // sym2 can only be a constant reference. let sym2: unique symbol = Symbol(); // Error: A variable whose type is a 'unique symbol' type must be 'const'. // Works - refers to a unique symbol, but its identity is tied to 'sym1'. let sym3: typeof sym1 = sym1; // Also works. class C { static readonly StaticSymbol: unique symbol = Symbol(); }
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
How can symbols be used as keys for object properties in TypeScript?
In TypeScript, symbols can be used as keys for object properties. This can be useful when you want to create private properties for an object, as the properties using symbols are not accessible for direct notation.
Here’s an example:
const sym = Symbol(); let obj = { [sym]: "value", }; console.log(obj[sym]); // "value"
In this example, sym
is a symbol that is used as a key
in the object obj
.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
What is the purpose of Symbol.isConcatSpreadable
?
Symbol.isConcatSpreadable
represents a boolean
value indicating that an object should be flattened to its array elements by Array.prototype.concat
. This symbol allows you to define if an object should be concatenated with others or if its elements should be concatenated individually.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
What is the purpose of Symbol.iterator
?
Symbol.iterator
represents a method that returns the default iterator for an object. This symbol is used to make an object iterable, and it’s called by the semantics of the for-of
statement.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
What is the purpose of Symbol.toPrimitive
?
Symbol.toPrimitive
represents a method that converts an object to a corresponding primitive value. This symbol is used when an object is coerced into a primitive type and it’s called by the toPrimitive
abstract operation. It allows you to define the behavior of an object when it’s converted to a primitive type.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
What is the purpose of Symbol.toStringTag
?
Symbol.toStringTag
is a string
value that is used in the creation of the default string description of an object. It is called by the built-in method Object.prototype.toString
. This symbol allows you to customize the default description of an object.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
What is the purpose of Symbol.match
?
Symbol.match
represents a regular expression method that matches the regular expression against a string. This symbol is called by the String.prototype.match
method, allowing you to define how string matching operations occur.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
What is the purpose of Symbol.replace
?
Symbol.replace
represents a regular expression method that replaces matched substrings of a string. This symbol is called by the String.prototype.replace
method, allowing you to customize the behavior of string replace operations.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
What is the purpose of Symbol.search
?
Symbol.search
represents a regular expression method that returns the index within a string that matches the regular expression. This symbol is called by the String.prototype.search
method, enabling you to define how string search operations behave.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
What is the purpose of Symbol.species
?
Symbol.species
is a function-valued property that is the constructor function used to create derived objects. This symbol allows you to define what constructor is used when creating derived objects, for example, when calling methods like map
, filter
or slice
which return a new instance of a class.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.
What is the purpose of Symbol.split
?
Symbol.split
represents a regular expression method that splits a string at the indices that match the regular expression. This symbol is called by the String.prototype.split
method, allowing you to control how a string is split into substrings.
“Documentation - Symbols” (typescriptlang.org). Retrieved July 13, 2023.