typed-set

Motivation
Unlike Typescript, type checking will be done at runtime.
Difference from the native Set
An array of initial values cannot be passed during initialization.
Installation
Install with npm:
$ npm install --save typed-set
Usage
TypedSet only works with the following function signatures
f(value: any) => boolean
Notice: if the function returns a non-boolean value, the result of the check will be skipped
const TypedSet = require("typed-set");
class TestClass {}
let classicSet = new TypedSet();
let stringTypedSet = new TypedSet("string");
let classTypedSet = new TypedSet(TestClass);
let anotherStringTypedSet = new TypedSet((value) => typeof value === "string");
let objectTypedSet = new TypedSet((value) => value instanceof TestClass);
let typedSet = new TypedSet((value) => [1, 2, 3].includes(value));
typedSet.add(1);
typedSet.add(2);
typedSet.add(3);
try {
typedSet.add("some incompatible type");
} catch (e) {
console.log(e.message);
}
List of predefined TypedSet classes for primitive types
- TypedSet.TypedBigIntSet
- TypedSet.TypedBooleanSet
- TypedSet.TypedFunctionSet
- TypedSet.TypedNumberSet
- TypedSet.TypedStringSet
- TypedSet.TypedSymbolSet
License
The typed-set package is open-sourced software licensed under the MIT license.