3 ms·
Since TS 3.4 you can have enum-like behaviour by using the `as const` annotation on object and array literals. const types = ['x', 'y', 'z'] as const;
by reaktivo 7y ago
Since TS 3.4 you can have enum-like behaviour by using the `as const` annotation on object and array literals.
const types = ['x', 'y', 'z'] as const;
const a: typeof types[number] = 'a'; // not ok
const b: typeof types[number] = 'z'; // ok
- styfle 7y agoNot to be confused with `const enum` which act more like a preprocessor and emits in the value at compile time. const enum Directions { Up, Down, Left, Right } console.log(Directions.Left) // emits js file with console.log(2) https://www.typescriptlang.org/docs/handbook/enums.html#const-enums https://www.typescriptlang.org/docs/handbook/enums.html#cons...