You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are three examples about enums like one, two, three. And I call function respond with different param. The second result puzzles me. I think TS will check type like the first example and tell me the problem.(Argument of type '"test"' is not assignable to parameter of type 'Response'.ts)The number 9 is not exit in Response but it not throw errors! How to ensure the type safety ? (I mean the respond params have to be number which include in the enum Response not 9 or others) Code
Any enum with only numeric values is mostly just an alias for number in practice--you can assign values that aren't in the enum. The reason for this is that enums are often used as bitfields, so it needs to be able to represent stuff like e.g. Flags.A | Flags.B | Flags.C.
If you want a more typesafe enum, you can try using string values instead of numbers.
TypeScript Version: ^3.5.1
Question :
There are three examples about enums like one, two, three. And I call function respond with different param. The second result puzzles me. I think TS will check type like the first example and tell me the problem.(Argument of type '"test"' is not assignable to parameter of type 'Response'.ts)The number 9 is not exit in Response but it not throw errors! How to ensure the type safety ? (I mean the respond params have to be number which include in the enum Response not 9 or others)
Code
Expected behavior:
The respond params have to be number which include in the enum Response and TS tell me how to fix it.
Actual behavior:
The example two doesn't throw error;
any: I just ask for stack overflow, but the effect is not good;
link: https://stackoverflow.com/questions/56849551/how-to-use-enum-in-react-typescript?answertab=active#tab-top
The text was updated successfully, but these errors were encountered: