node-http-status icon indicating copy to clipboard operation
node-http-status copied to clipboard

Make the name type definition explicit (and not just string)

Open codenomnom opened this issue 1 year ago • 9 comments

Summary

As of now, the definition of each status name looks like this:

readonly '100_NAME': string;

Can it be explicit, like:

readonly '100_NAME': 'CONTINUE';

Motivation

The reason for that request is that when we use the name for generic mapping, TS can't properly infer it:

const name = status[`${status.TOO_MANY_REQUESTS}_NAME`];
// its value is indeed "TOO_MANY_REQUESTS"
// but the type is simply "string" (any string)
// which is technically true, and yet cumbersome

// because of that, creating a map doesn't work as best as it could:
const map = {
  [`${status.TOO_MANY_REQUESTS}_NAME`]: 'custom message or something',
}

// IDE cannot figure out that map should contain "TOO_MANY_REQUESTS" key

Alternative

Duplicate names as keys, but that doesn't feel right.

codenomnom avatar Oct 01 '24 09:10 codenomnom