Skip to content

Instantly share code, notes, and snippets.

@low-ghost
Last active June 18, 2016 12:48
Show Gist options
  • Save low-ghost/c5b321b821043fdda15b00c7715e3918 to your computer and use it in GitHub Desktop.
Save low-ghost/c5b321b821043fdda15b00c7715e3918 to your computer and use it in GitHub Desktop.
quick ts/js enum using symbols and class getters
import { forEach, set } from 'lodash';
export class Enum <T>{
constructor(...values) {
forEach(values, (value) => set(this, value, Symbol(value));
}
get (value: T): Symbol {
return this[value];
}
}
const Y = new Enum('TEST1', 'TEST2', 'TEST3');
Y.TEST1; // => Symbol(Test1)
Y.TEST1 === Y.TEST2; // => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment