I'm struggling to get type safety when using enum as function parameters. For example, using the example code in the TS Enum docs:
enum UserResponse {
No = 0,
Yes = 1,
}
function respond(recipient: string, message: UserResponse): void {
console.log(recipient, message)
}
respond("Princess Caroline", UserResponse.Yes);
respond("Princess Caroline", 'some other message'); // Argument of type '"some other message"' is not assignable to parameter of type 'UserResponse'.
respond("Princess Caroline", 5); // No error...
- Why is the compiler not giving errors when using the value
5which is not defined inUserResponse? - How can I improve the typings so that the
messageparam ONLY acceptsUserResponse.Yes | UserResponse.No? - Should I be using some other data structure to hold
UserResponse?