| 1 |
enum LogLevel { |
| 2 |
ERROR, |
| 3 |
WARN, |
| 4 |
INFO, |
| 5 |
DEBUG, |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* This is equivalent to: |
| 10 |
* type LogLevelStrings = 'ERROR' | 'WARN' | 'INFO' | 'DEBUG'; |
| 11 |
*/ |
| 12 |
type LogLevelStrings = keyof typeof LogLevel; |
| 13 |
|
| 14 |
function printImportant(key: LogLevelStrings, message: string) { |
| 15 |
const num = LogLevel[key]; |
| 16 |
if (num <= LogLevel.WARN) { |
| 17 |
console.log("Log level key is:", key); |
| 18 |
console.log("Log level value is:", num); |
| 19 |
console.log("Log level message is:", message); |
| 20 |
} |
| 21 |
} |
| 22 |
printImportant("ERROR", "This is a message"); |
| 23 |
|
| 24 |
// From https://www.typescriptlang.org/docs/handbook/enums.html |
| 25 |
|