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;
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号