-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the name type definition explicit (and not just string) #50
Comments
I am currently in the process of migrating a few of my package into TS, I could certainly have http-status included in the list and will look in the coming days. I think your suggestion makes sense. |
I just release version 1.8 which is a typescript rewrite. Please let me know if it corresponds to your expectations. |
Two new versions are published:
|
@codenomnom could you take the time to validate version 2 and close the issue if it works accordingly to your expectations. Thank you. |
Hey there, sorry for the delay, got sick 🤧 Will ping you shortly, thanks! |
@wdavidw thank you for taking time to ping me with the update. I'm not in my brightest shape, but I'm currently missing on how to deal with this use case of mine: // some error handling catch function
return {
success: false,
code: err.statusCode,
text: HTTPStatuses[err.statusCode],
} It's like a generic map between some error I received (regardless from where), and I want to translate 401 to Unfortunately I get this TS error:
Regardless of usage, I get similar errors: HTTPStatuses[`${err.status}_MESSAGE`]
Sorry for not being the brightest I could 😅 |
I tried to import using
And my linter told me
I'm using v2.0.0 This is my config
|
@codenomnom, I hope you will get better. I tried that code and it seems to work fine: import status from "http-status";
const getError = function (statusCode: number) {
return {
success: false,
code: statusCode,
text: status[statusCode],
};
};
console.log(getError(400)); node --import tsx samples/issue.50.ts
{
success: false,
code: 400,
text: 'Bad Request',
message: 'The server cannot or will not process the request due to an apparent client error.'
} |
@realtebo I you can make the change, try updating your "module" definition to "ESNext" or "NodeNext". Not a specialist in that field but it could help. |
Summary
As of now, the definition of each status name looks like this:
Can it be explicit, like:
Motivation
The reason for that request is that when we use the name for generic mapping, TS can't properly infer it:
Alternative
Duplicate names as keys, but that doesn't feel right.
The text was updated successfully, but these errors were encountered: