Build an Enum Object from a List of Names
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Instead of a TypeScript enum, many codebases keep a list of names and build an object from it. Export a type EnumOf<T> that turns a tuple of strings into an object whose keys and values are those exact strings, and write createEnum(names): return an object mapping each name to itself, in order. Type it so createEnum(['draft', 'live'] as const) is { draft: 'draft'; live: 'live' }, reading a name that isn't in the list is a type error, and only strings are accepted.
Your code must pass
- Maps each name to itself, in order
- Returns an empty object for no names
- Has no type errors
- Keys and values are the exact names
- Rejects a name that isn't in the list
- Only takes strings