In TypeScript, you can copy a Set by creating a new Set and using the spread operator or the Set constructor.
In this example,
originalSet
with some elements.copiedSet
using the spread operator [...originalSet]
.let originalSet: Set<string> = new Set(["apple", "banana", "cherry"]);
let copiedSet: Set<string> = new Set([...originalSet]);
console.log("Original Set: ", originalSet);
console.log("Copied Set: ", copiedSet);
Original Set: Set(3) { 'apple', 'banana', 'cherry' } Copied Set: Set(3) { 'apple', 'banana', 'cherry' }
In this example,
originalSet
with some elements.copiedSet
using the Set constructor new Set(originalSet)
.let originalSet: Set<string> = new Set(["apple", "banana", "cherry"]);
let copiedSet: Set<string> = new Set(originalSet);
console.log("Original Set: ", originalSet);
console.log("Copied Set: ", copiedSet);
Original Set: Set(3) { 'apple', 'banana', 'cherry' } Copied Set: Set(3) { 'apple', 'banana', 'cherry' }
In this tutorial, we learned How to Copy a Set in TypeScript language with well detailed examples.