Imagine having a group of values that you want to protect from being edited, as shown below:
// These values should not be editable.
const listenedKeys = new Set(['w', 'a', 's', 'd'])
// This value can be accessed without issues.
const hasA = listenedKeys.has('a')
// Attempting to modify this should cause an error.
listenedKeys.add('r')
Is it possible to make this set immutable in TypeScript? If so, how?
I've attempted using the Readonly
utility type, but it did not prevent me from making changes to the set:
const listenedKeys: Readonly<Set<string>> = new Set(['w', 'a', 's', 'd'])
// No errors occur during this operation
listenedKeys.add('r')