Created
September 29, 2021 19:53
-
-
Save oreillyross/d5f10c5fbaa563426372b20e5b2961ad to your computer and use it in GitHub Desktop.
Creating a typesafe enum using the Symbol operator and typeof
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
'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