Capitalize
介绍
实现 Capitalize<T>
它将字符串的第一个字母转换为大写,其余字母保持原样。
例如
ts
type capitalized = MyCapitalize<'hello world'> // expected to be 'Hello world'
View on GitHubts
type capitalized = MyCapitalize<'hello world'> // expected to be 'Hello world'
起点
ts
/* _____________ Your Code Here _____________ */typeMyCapitalize <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'foobar'>, 'Foobar'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'FOOBAR'>, 'FOOBAR'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'foo bar'>, 'Foo bar'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <''>, ''>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'a'>, 'A'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'b'>, 'B'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'c'>, 'C'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'d'>, 'D'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'e'>, 'E'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'f'>, 'F'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'g'>, 'G'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'h'>, 'H'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'i'>, 'I'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'j'>, 'J'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'k'>, 'K'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'l'>, 'L'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'m'>, 'M'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'n'>, 'N'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'o'>, 'O'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'p'>, 'P'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'q'>, 'Q'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'r'>, 'R'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'s'>, 'S'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'t'>, 'T'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'u'>, 'U'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'v'>, 'V'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'w'>, 'W'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'x'>, 'X'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'y'>, 'Y'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'z'>, 'Z'>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeMyCapitalize <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'foobar'>, 'Foobar'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'FOOBAR'>, 'FOOBAR'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'foo bar'>, 'Foo bar'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <''>, ''>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'a'>, 'A'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'b'>, 'B'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'c'>, 'C'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'d'>, 'D'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'e'>, 'E'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'f'>, 'F'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'g'>, 'G'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'h'>, 'H'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'i'>, 'I'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'j'>, 'J'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'k'>, 'K'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'l'>, 'L'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'m'>, 'M'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'n'>, 'N'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'o'>, 'O'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'p'>, 'P'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'q'>, 'Q'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'r'>, 'R'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'s'>, 'S'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'t'>, 'T'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'u'>, 'U'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'v'>, 'V'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'w'>, 'W'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'x'>, 'X'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'y'>, 'Y'>>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Equal <MyCapitalize <'z'>, 'Z'>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
//方法1// 注意: 原答案链接中,讨论了`${infer F}${infer Rest}` 和 `${infer F}${infer T}${infer Rest}`的区别. TS版本5测试来看,已经没有之前的特征typeazToAZ = {a : 'A',b : 'B',c : 'C',d : 'D',e : 'E',f : 'F',g : 'G',h : 'H',i : 'I',j : 'J',k : 'K',l : 'L',m : 'M',n : 'N',o : 'O',p : 'P',q : 'Q',r : 'R',s : 'S',t : 'T',u : 'U',v : 'V',w : 'W',x : 'X',y : 'Y',z : 'Z'}typeUpperCase <T extends string> =T extends keyofazToAZ ?azToAZ [T ] :T ;typeMyCapitalize <T extends string> =T extends `${inferF }${inferRest }` ? `${UpperCase <F >}${Rest }` :T ;
ts
//方法1// 注意: 原答案链接中,讨论了`${infer F}${infer Rest}` 和 `${infer F}${infer T}${infer Rest}`的区别. TS版本5测试来看,已经没有之前的特征typeazToAZ = {a : 'A',b : 'B',c : 'C',d : 'D',e : 'E',f : 'F',g : 'G',h : 'H',i : 'I',j : 'J',k : 'K',l : 'L',m : 'M',n : 'N',o : 'O',p : 'P',q : 'Q',r : 'R',s : 'S',t : 'T',u : 'U',v : 'V',w : 'W',x : 'X',y : 'Y',z : 'Z'}typeUpperCase <T extends string> =T extends keyofazToAZ ?azToAZ [T ] :T ;typeMyCapitalize <T extends string> =T extends `${inferF }${inferRest }` ? `${UpperCase <F >}${Rest }` :T ;
ts
// most populartypeMyCapitalize <S extends string> =S extends `${inferx }${infertail }` ? `${Uppercase <x >}${tail }` :S ;
ts
// most populartypeMyCapitalize <S extends string> =S extends `${inferx }${infertail }` ? `${Uppercase <x >}${tail }` :S ;
ts
// second popularinterfaceToUpperCase {a : "A"b : "B"c : "C"d : "D"e : "E"f : "F"g : "G"h : "H"i : "I"j : "J"k : "K"l : "L"m : "M"n : "N"o : "O"p : "P"q : "Q"r : "R"s : "S"t : "T"u : "U"v : "V"w : "W"x : "X"y : "Y"z : "Z"}typeLowerCase = keyofToUpperCase typeMyCapitalize <S extends string> =S extends `${inferFirst extendsLowerCase }${inferRest }` ? `${ToUpperCase [First ]}${Rest }` :S
ts
// second popularinterfaceToUpperCase {a : "A"b : "B"c : "C"d : "D"e : "E"f : "F"g : "G"h : "H"i : "I"j : "J"k : "K"l : "L"m : "M"n : "N"o : "O"p : "P"q : "Q"r : "R"s : "S"t : "T"u : "U"v : "V"w : "W"x : "X"y : "Y"z : "Z"}typeLowerCase = keyofToUpperCase typeMyCapitalize <S extends string> =S extends `${inferFirst extendsLowerCase }${inferRest }` ? `${ToUpperCase [First ]}${Rest }` :S