TypeScript Reference ยท Utility Types
TypeScript Uncapitalize<S> utility type
Reference for Uncapitalize<S> in TypeScript: an intrinsic type that lower-cases only the first character of a string type, useful when stripping prefixes.
Uncapitalize<S> lower-cases the first character of a string type and leaves the others unchanged. Uncapitalize<"Name"> is "name"; Uncapitalize<"NAME"> is "nAME".
The usual pattern is the reverse of Capitalize: you have names such as getName or onClick and want the underlying property or event name. Strip the prefix with a template literal and infer, then uncapitalize what remains.
type Prop<T extends string> = T extends `get${infer Rest}` ? Uncapitalize<Rest> : never; type P = Prop<"getName">; // "name"
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 Uncapitalize<S extends string> = intrinsic;
| Name | Description |
|---|---|
S |
A string type whose first character should be lower-cased. |
Returns: S with its first character mapped to lower case.
Edge cases:
Uncapitalize<"">is"".- Unions distribute:
Uncapitalize<"A" | "B">is"a" | "b". - It does not recognise acronyms:
Uncapitalize<"URLPath">is"uRLPath", not"urlPath". - Strings that already start with a lower-case letter, a digit or a symbol are unchanged.
Usage notes
- Combine with
inferin a template literal to strip a prefix or suffix, then fix the case of what is left. - When remapping keys in a mapped type,
as Uncapitalize<K>is the way to turnPascalCaseinterfaces intocamelCaseones. - Because acronyms are not handled, do not rely on this for converting names like
HTTPServer; write the conversion at runtime and validate it with tests. - The type exists for compile-time checking only. Pair every use with a runtime transformation you control.
- Added in TypeScript 4.1 with the other intrinsic string types.
Related
- Capitalize
, Lowercase, Uppercase - Tutorial: Template literal types, Conditional types