We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.
Required fields*
9
7
Here's this solution in TypeScript, where the type returned is the capitalized word, rather than string: const capitalize = <T extends string>(s: T) => (s[0].toUpperCase() + s.slice(1)) as Capitalize<typeof s>;
This is mentioned in answers below, but worth pointing out here: this answer does not work for all Unicode text. Passing "𐐨𐑍𐑏" to this function returns the same string, but it should return "𐐀𐑍𐑏". That's because charAt splits at UTF16 code units. There are characters with case-folding definitions in the SMP that are encoded with two UTF16 code units. While simple, this shouldn't be used.
A tag is a keyword or label that categorizes your question with other, similar questions. Choose one or more (up to 5) tags that will help answerers to find and interpret your question.
complete the sentence: my question is about...
use tags that describe things or concepts that are essential, not incidental to your question
string:const capitalize = <T extends string>(s: T) => (s[0].toUpperCase() + s.slice(1)) as Capitalize<typeof s>;charAtsplits at UTF16 code units. There are characters with case-folding definitions in the SMP that are encoded with two UTF16 code units. While simple, this shouldn't be used."flat"is returned as"FLat".