Last active
January 13, 2023 08:46
-
-
Save erkobridee/576bcba33ed5fcf26c68fb0f32efdef3 to your computer and use it in GitHub Desktop.
a sample code of how to use const as const instead of enums in TypeScript
This file contains 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
/* | |
from: | |
Enums considered harmful | Matt Pocock | |
https://www.youtube.com/watch?v=jjMbPt_H3RQ | |
*/ | |
const LogLevel = { | |
DEBUG: "DEBUG", | |
INFO: "INFO", | |
WARNING: "WARNING", | |
ERROR: "ERROR" | |
} as const; | |
type TLogLevelKeys = keyof typeof LogLevel; | |
type TLogLevel = typeof LogLevel[TLogLevelKeys]; | |
const log = (message: string, level: TLogLevel = LogLevel.DEBUG) => console.log(`${LogLevel[level]}: ${message}`); | |
log("Hello World", LogLevel.DEBUG); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
code at the typescript playground