space-lift
Version:
TypeScript Array, Object, Map, Set, Union, Enum utils
13 lines (12 loc) • 404 B
JavaScript
/**
* Creates a type safe string enumeration from a list of strings, providing:
* the list of all possible values, an object with all enum keys and the derived type of the enum in a single declaration.
*/
export function createEnum(...values) {
const enumeration = {};
values.forEach(v => (enumeration[v] = v));
return {
enum: enumeration,
values: new Set(values)
};
}