Typescript类型体操 - Capitalize

题目

中文

实现 Capitalize<T> 它将字符串的第一个字母转换为大写,其余字母保持原样。

例如

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

English

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'

答案

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

在线演示

posted @ 2022-09-05 19:41  Laggage  阅读(276)  评论(0编辑  收藏  举报