跳到主要内容

Capitalize

介绍

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

例如

ts
type capitalized = MyCapitalize<'hello world'> // expected to be 'Hello world'
ts
type capitalized = MyCapitalize<'hello world'> // expected to be 'Hello world'
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type MyCapitalize<T> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
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'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
ts
/* _____________ Your Code Here _____________ */
 
type MyCapitalize<T> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
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'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
//方法1
 
// 注意: 原答案链接中,讨论了`${infer F}${infer Rest}` 和 `${infer F}${infer T}${infer Rest}`的区别. TS版本5测试来看,已经没有之前的特征
type azToAZ = {
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'
}
 
type UpperCase<T extends string> = T extends keyof azToAZ ? azToAZ[T] : T;
 
type MyCapitalize<T extends string> = T extends `${infer F}${infer Rest}` ? `${UpperCase<F>}${Rest}` : T;
 
ts
//方法1
 
// 注意: 原答案链接中,讨论了`${infer F}${infer Rest}` 和 `${infer F}${infer T}${infer Rest}`的区别. TS版本5测试来看,已经没有之前的特征
type azToAZ = {
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'
}
 
type UpperCase<T extends string> = T extends keyof azToAZ ? azToAZ[T] : T;
 
type MyCapitalize<T extends string> = T extends `${infer F}${infer Rest}` ? `${UpperCase<F>}${Rest}` : T;
 
ts
// most popular
type MyCapitalize<S extends string> = S extends `${infer x}${infer tail}` ? `${Uppercase<x>}${tail}` : S;
 
ts
// most popular
type MyCapitalize<S extends string> = S extends `${infer x}${infer tail}` ? `${Uppercase<x>}${tail}` : S;
 
ts
// second popular
interface ToUpperCase {
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"
}
 
type LowerCase = keyof ToUpperCase
type MyCapitalize<S extends string> = S extends `${infer First extends LowerCase}${infer Rest}` ? `${ToUpperCase[First]}${Rest}` : S
 
ts
// second popular
interface ToUpperCase {
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"
}
 
type LowerCase = keyof ToUpperCase
type MyCapitalize<S extends string> = S extends `${infer First extends LowerCase}${infer Rest}` ? `${ToUpperCase[First]}${Rest}` : S
 
view more solutions