Skip to content

Commit

Permalink
Merge pull request phuocng#530 from jjswifty/feat/better-typed-capita…
Browse files Browse the repository at this point in the history
…lize

feat(string/capitalize): add better type annotation for capitalize
  • Loading branch information
phuocng committed Apr 11, 2023
2 parents cff7d51 + 12a55f4 commit 14bbafe
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions contents/string/capitalize-a-string.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ const capitalize = (str) => str.replace(/^([a-z])/, (first) => first.toUpperCase
**TypeScript version**

```js
const capitalize = (str: string): string => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
const capitalize1 = <T extends string,>(str: T) => `${str.charAt(0).toUpperCase()}${str.slice(1)}` as Capitalize<T>;

// Or
const capitalize = ([first, ...rest]: string): string => `${first.toUpperCase()}${rest.join('')}`;
const capitalize2 = <T extends string,>([first, ...rest]: T) => `${first.toUpperCase()}${rest.join('')}` as Capitalize<T>;

// Or
const capitalize = (str: string): string => str.replace(/^([a-z])/, (first) => first.toUpperCase());
const capitalize3 = <T extends string,>(str: T) => str.replace(/^([a-z])/, (first) => first.toUpperCase()) as Capitalize<T>;
```

**Examples**
Expand Down

0 comments on commit 14bbafe

Please sign in to comment.