Skip to content

Variable: capitalize()

const capitalize: <StringType>(str) => Capitalize<StringType>

Capitalizes the first letter of a string while preserving the rest. Results are cached for performance and provides precise TypeScript typing.

Type Parameters

StringType

StringType extends string

The input string type for precise return typing

Parameters

str

StringType

The string to capitalize

Returns

Capitalize<StringType>

The string with the first letter capitalized

Example

typescript
console.log(capitalize('hello'))     // 'Hello'
console.log(capitalize('wORLD'))     // 'WORLD'
console.log(capitalize(''))          // ''
console.log(capitalize('a'))         // 'A'

// TypeScript preserves literal types
const result: 'Hello' = capitalize('hello' as const)

Released under the MIT License.