tags | |
---|---|
|
Better to avoid using of enums
Reasons:
- Runtime object is not that plain "key-value" pair as it in TS
- Enums is working as [[ts-nominal-type-system|nominal type system]]. That means we have to import it everywhere, and won't be able pass value as simple string:
log('Hey', 'DEBUG');
Use plain object with [[ts-const-assertion|const assertion]]:
const LogLevel = {
DEBUG: 'DEBUG',
WARNING: 'WARNING',
ERROR: 'ERROR',
} as const
type ObjectValues<T> = T[keyof T];
type LogLevel = ObjectValues<typeof LogLevel>;
function log(message: string, level: LogLevel) {
}
log('Hey', 'DEBUG');
[[Matt Pocock|Matt Pocock's]] video: https://www.youtube.com/watch?v=jjMbPt_H3RQ&list=WL&index=6