Set Object in ES7 and TypeScript

The JavaScript Set object is a versatile tool for managing collections with unique values, accommodating various data types, including both primitive values and object references. In this article, we'll dive into the capabilities of the Set object in ES7 (ECMAScript 2016) and explore its usage in TypeScript.

JavaScript Set Object in ES7

const mySet = new Set([iterable]);

Parameter

  • iterable: Represents an iterable object whose elements will be added to the new Set.

Key Points

  1. Internal Key Concept: A Set object utilizes the concept of keys internally to ensure uniqueness.
  2. Unique Values: A Set object cannot contain duplicate values, making it ideal for handling distinct elements.
  3. Iteration Order: The Set object iterates its elements in insertion order, maintaining the sequence in which elements were added.

JavaScript Set Methods

Let's explore the core methods provided by the Set object along with their descriptions.

  1. add(value): Adds the specified values to the Set object.
  2. clear(): Removes all elements from the Set object.
  3. delete(value): Deletes the specified element from the Set object.
  4. entries(): Returns an object of Set iterator containing an array of [value, value] for each element.
  5. forEach(callback): Executes the specified function once for each value in the Set.
  6. has(value): Indicates whether the Set object contains the specified value element.
  7. values(): Returns an object of Set iterator containing the values for each element.

TypeScript and ES7

Now, let's adapt the JavaScript Set object to TypeScript, incorporating the enhanced features provided by ES7.

class CustomSet<T> {
    private set: Set<T>;

    constructor(iterable?: Iterable<T>) {
        this.set = new Set(iterable);
    }

    add(value: T): void {
        this.set.add(value);
    }

    clear(): void {
        this.set.clear();
    }

    delete(value: T): boolean {
        return this.set.delete(value);
    }

    entries(): IterableIterator<[T, T]> {
        return this.set.entries();
    }

    forEach(callback: (value: T) => void): void {
        this.set.forEach(callback);
    }

    has(value: T): boolean {
        return this.set.has(value);
    }

    values(): IterableIterator<T> {
        return this.set.values();
    }
}

// Example Usage
const myTypedSet = new CustomSet<number>([1, 2, 3]);
myTypedSet.add(4);
myTypedSet.forEach(value => console.log(value)); // Output: 1, 2, 3, 4

In this TypeScript adaptation, we define a CustomSet the class that encapsulates the Set object. The class takes advantage of TypeScript generics to ensure type safety. The methods mirror those of the native Set object while providing a typed interface.

The JavaScript Set object stands as a versatile guardian for handling collections with unparalleled uniqueness. As we explored its intricacies in both ES7 and TypeScript, a panorama of powerful features unfolded. The Set object, with its internal key concept, ensures the uniqueness of values, a critical aspect in scenarios demanding distinct elements. Its commitment to maintaining insertion order during iteration adds a layer of reliability to data processing. As we transitioned into TypeScript, the Set object seamlessly embraced the language's capabilities. The introduction of the CustomSet class, leveraging TypeScript generics, fortified type safety, and ensuring a robust foundation for developers.

In the evolving world of ECMAScript standards, ES7 enriches our toolset, providing concise and expressive ways to interact with the Set object. This article aimed to bridge the gap between JavaScript and TypeScript, presenting a unified approach for harnessing the power of Sets in modern web development. Whether you're working in a traditional JavaScript environment or adopting the type safety of TypeScript, the Set object emerges as a stalwart companion, simplifying the complexities of managing collections with unique values. Embrace its capabilities, understand its nuances, and empower your code with the efficiency and elegance it deserves.


Similar Articles