0

I have this code:

enum ResponseStatus {
    BAD_REQUEST = 400,
    INTERNAL_SERVER = 500
}

const status: ResponseStatus = -122;

I can assign any number to status and no error is generated, although status is of type ResponseStatus which can be either 400 or 500.

So what is the point of restricting status to the ResponseStatus type if I can assign any number to it anyway?

This behavior does not happen with enums if the values are strings.

Why is this implemented in TypeScript this way?

If I restrict something to a type I expect to not be able to assign stuff that is not compatible with that type.

Please provide a simple example if you can, which explains the reason for this. I read something online that it has something to do with bitwise operators but I really didn't understand what and where it is useful.

whitefang1993
  • 1,666
  • 3
  • 16
  • 27

0 Answers0