Disclosure

Most of the problems under my TypeHero Challenges folder were either obtained from typehero.dev or from type-challenges repo. Purpose of these articles are just to document my approaches for my easy reference. Please visit the respective links for more info.

Link to original

Problem Description

Implement Capitalize<T> which converts the first letter of a string to uppercase and leave the rest as-is.

For example:

type capitalized = Capitalize<'hello world'> // expected to be 'Hello world'

Solutions

Approach 1: Using Uppercase built-in utility type

type MyCapitalize<S extends string> = S extends `${infer FirstLetter}${infer Rest}` ? `${Uppercase<FirstLetter>}${Rest}` : S;