TypeScript Reference ยท Utility Types
TypeScript Lowercase<S> utility type
Reference for Lowercase<S> in TypeScript: an intrinsic string type that converts literal and template literal types to lower case, with normalisation examples.
Lowercase<S> maps every letter of a string literal type to lower case. Lowercase<"HELLO"> is "hello". It distributes over unions and works inside template literal types.
It is the counterpart of Uppercase and, like it, is implemented inside the compiler. A common use is accepting input in any case at the type level while storing a normalised key, for example header names, file extensions or enum-like strings.
type Header = "Content-Type" | "ACCEPT"; type Normalized = Lowercase<Header>; // "content-type" | "accept"
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 Lowercase<S extends string> = intrinsic;
| Name | Description |
|---|---|
S |
A string literal type, union of literals, template literal type, or type parameter constrained to string. |
Returns: the string type with each character mapped to lower case.
Edge cases:
- Unions distribute:
Lowercase<"A" | "B">is"a" | "b". - Characters without case, such as digits,
-and_, are unchanged. Lowercase<string>is a special type, not plainstring. It is assignable tostringbut not the other way round.
Usage notes
- This changes types only. Call
toLowerCase()for the value and use an assertion to tell the compiler about the literal, as in the first example. - Normalising the key type and the value together keeps lookups safe: accept
E extends string, convert withLowercase<E>, then intersect with the known keys. - Do not expect locale-specific rules: the Turkish dotted and dotless i, for example, are not modelled.
- Used with key remapping,
as Lowercase<K>renames the properties of an object type; check for collisions, because two keys that differ only by case would merge into one. - Introduced in TypeScript 4.1 together with template literal types.
Related
- Uppercase
, Capitalize, Uncapitalize - Tutorial: Template literal types, Literal types