Sounds like you could use a utility we wrote. It doesn't create an enum per se, but it does create a type-safe object where the keys have their name as their values and use keyof typeof
for a bit of string-type safety.
This was created before string enums existed, which is why is called an enum, but isn't really an enum. It's just an object, but you can use it instead of hardcoding strings.
/**
* This creates a fake string enum like object. Use like so:
* const Ab = strEnum(['a', 'b']);
* type AbKeys = keyof typeof Ab;
* @param keys keys in the enum
* @returns enum object
*/
export function createStringEnum<T extends string>(keys: T[]): {[K in T]: K} {
return keys.reduce((res, key) => {
res[key] = key;
return res;
}, Object.create(null));
}
const Ab = createStringEnum(['a', 'b']);
type AbKeys = keyof typeof Ab;
const Bc = createStringEnum(['b', 'c']);
type BcKeys = keyof typeof Ab;
console.log(Bc.blah) // Compilation error blah property does not exist
// Error invalid string
const b: AbKeys = "e";
// An enum throws an error, but this isn't really an enum
// Main drawback of this approach
const a: AbKeys = Bc.b;
Even if it does not fit your needs, this could be helpful to others that aren't required to use enums.