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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

Capitalize<StringType>

Parameters

ParameterDescription
StringType A string literal type.

Return value

The literal with its first letter upper-cased.

Examples

Event handler namesTry it yourself
type Event = "click" | "focus";
type Handler = `on${Capitalize<Event>}`; // "onClick" | "onFocus"
const h: Handler = "onFocus";
console.log(h);

Output

onFocus
Setter namesTry it yourself
type Field = "name" | "email";
type Setter = `set${Capitalize<Field>}`;
const setters: Setter[] = ["setName", "setEmail"];
console.log(setters);

Output

[ 'setName', 'setEmail' ]

Version history

VersionChange
TypeScript 4.1Added

Was this page helpful?