Skip to content

Instantly share code, notes, and snippets.

@erkobridee
Last active January 13, 2023 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erkobridee/576bcba33ed5fcf26c68fb0f32efdef3 to your computer and use it in GitHub Desktop.
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
/*
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);
@erkobridee
Copy link
Author

erkobridee commented Dec 16, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment