Last active
June 18, 2016 12:48
-
-
Save low-ghost/c5b321b821043fdda15b00c7715e3918 to your computer and use it in GitHub Desktop.
quick ts/js enum using symbols and class getters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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