TypeScript 7 reference · Utility Types
TypeScript Capitalize<StringType> Utility Type
Upper-case the first character of a string literal type: Capitalize<'click'> is 'Click'. Used to build names like onClick.
TypeScript 7 Since 4.1
Syntax
Capitalize<StringType>
Parameters
| Parameter | Description |
|---|---|
StringType |
A string literal type. |
Return value
The literal with its first letter upper-cased.
Examples
type Event = "click" | "focus";
type Handler = `on${Capitalize<Event>}`; // "onClick" | "onFocus"
const h: Handler = "onFocus";
console.log(h);
Output
onFocus
type Field = "name" | "email";
type Setter = `set${Capitalize<Field>}`;
const setters: Setter[] = ["setName", "setEmail"];
console.log(setters);
Output
[ 'setName', 'setEmail' ]
Version history
| Version | Change |
|---|---|
| TypeScript 4.1 | Added |
Related
Uppercase<StringType>· Turn a string literal type into upper case at the type level: Uppercase<'get'> is 'GET'.template literal types· Build string literal types with template syntax: `on${string}` or combinations of unions like `${Size}-${Color}`.- TypeScript Utility Types · tutorial