What you want is not directly possible. When you do keyof typeof
you create a union type 'CREATED' | 'OK' | 'NOT_FOUND' ...
that is completely separate from the enum it once was.
The closest you can get is by doing
function sendStatus(code: HttpStatus) {
// code
}
send(HttpStatus.OK) // This will autocomplete and show the status code numbers
and then converting the code to string inside the sendStatus
function.
It's hard to say what you really want without knowing the exact usage you're looking for, but I would consider just having a plain old object instead of the enum
const HTTP_STATUS = {'OK':200, 'CREATED':201} as const
Then, if you need to, you can also create both of the types like so
type StringStatus = keyof typeof HTTP_STATUS // 'OK' | 'CREATED'
type NumsStatus = (typeof HTTP_STATUS)[keyof typeof HTTP_STATUS] // 200 | 201
Generally there's rarely a good reason to use enum
s in modern TS. Usually objects and/or union types do the job much better.