TypeScript Reference ยท Utility Types
TypeScript Uppercase<S> utility type
Reference for Uppercase<S> in TypeScript: an intrinsic string type that converts literal types and template literal types to upper case, with examples.
Uppercase<S> turns a string literal type into the same literal with every letter in upper case. Uppercase<"get"> is "GET". It works on unions (each member is converted), on template literal types, and on type parameters that extend string.
It is one of four intrinsic string-manipulation types, together with Lowercase, Capitalize and Uncapitalize. They are implemented inside the compiler rather than written in TypeScript, because there is no way to express case conversion with conditional types alone. Typical uses are building constant-style names, HTTP method unions and event-key mappings.
type Verb = "get" | "post"; type Method = Uppercase<Verb>; // "GET" | "POST"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
type Uppercase<S extends string> = intrinsic;
| Name | Description |
|---|---|
S |
A string type: a literal, a union of literals, a template literal type or a type parameter constrained to string. |
Returns: the string type with each character mapped to upper case using the same Unicode rules as String.prototype.toUpperCase at the type level.
Edge cases:
- A union distributes:
Uppercase<"a" | "b">is"A" | "B". - Characters with no upper-case form are unchanged:
Uppercase<"a-1">is"A-1". Uppercase<string>is not plainstring: it is a narrower type that is assignable tostringbut not the other way round. A literal that is already upper case, such as"OPEN", is accepted; an arbitrarystringneeds an assertion.- The type-level mapping is locale-independent, like
toUpperCase()(usetoLocaleUpperCase()when you need locale rules at runtime).
Usage notes
- The type is not applied to values:
"abc".toUpperCase()returnsstring, not"ABC". You need an assertion or a typed wrapper to carry the literal type through. - Use
keyof T & stringin key remapping, because keys may benumberorsymbol. - Combine with template literal types to build families of names:
`on${Capitalize<E>}`for handlers,`${Uppercase<M>}_REQUEST`for actions. - Applying it twice is idempotent; applying it to something already upper case changes nothing.
- It has no runtime output; the generated JavaScript contains none of these types.
Related
- Lowercase
, Capitalize, Uncapitalize - Tutorial: Template literal types, Mapped types