Skip to content

Instantly share code, notes, and snippets.

@oreillyross
Created September 29, 2021 19:53
Show Gist options
  • Save oreillyross/d5f10c5fbaa563426372b20e5b2961ad to your computer and use it in GitHub Desktop.
Save oreillyross/d5f10c5fbaa563426372b20e5b2961ad to your computer and use it in GitHub Desktop.
Creating a typesafe enum using the Symbol operator and typeof
'use strict'
const on = Symbol('on')
const off = Symbol('off')
type switchState = typeof on | typeof off
function changeSwitch(switchState: switchState) {
switch (switchState) {
case on:
return 'Light is on'
case off:
return 'Light is off'
}
}
console.log(changeSwitch(off))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment