Home > Mobile >  Convert number literal type to string literal type
Convert number literal type to string literal type

Time:04-05

I've seen questions about converting string literal types to number types (TypeScript: is there a way to convert a string literal type to a number type?), but not the other way around. Is there a way to define a type type NumberToString<N extends number> such that e.g. NumberToString<42> is 42?

The purpose of this is to provide a return type for Object.keys (in cases where I'm really, really sure that there are no extra properties).

CodePudding user response:

Much much much simpler now with template literal types:

type NumberToString<N extends number> = `${N}`;
  • Related