Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No error on value outside TypeScript enum

Was hoping this might produce a compile time error but I guess I have just misunderstood how enums work...

enum SortDirection {
    ascending = 1,
    descending = -1
}
type IndexSpec = {[index: string]: SortDirection};

var i: IndexSpec = {thing: 3};  // no compile time error
like image 385
AJP Avatar asked Jan 26 '26 00:01

AJP


1 Answers

All numeric values are considered value enum values.

This is allowed because there's no distinction between flag and non-flag enums:

enum MyFlags {
  Cool = 0x1,
  Awesome = 0x2,
  Neat = 0x4
}

var i: MyFlags = 5; // Cool | Neat
like image 140
Ryan Cavanaugh Avatar answered Jan 29 '26 00:01

Ryan Cavanaugh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!