00110-medium-capitalize

Back

type MyCapitalize<Text extends string> = Text extends `${infer First}${infer Rest}`
    ? `${Uppercase<First>}${Rest}`
    : Text

Solution by HrOkiG2 #35267

type Capitalize<C extends string> = 
  C extends 'a' ? 'A' 
: C extends 'b' ? 'B' 
: C extends 'c' ? 'C' 
: C extends 'd' ? 'D' 
: C extends 'e' ? 'E' 
: C extends 'f' ? 'F' 
: C extends 'g' ? 'G' 
: C extends 'h' ? 'H' 
: C extends 'i' ? 'I' 
: C extends 'j' ? 'J' 
: C extends 'k' ? 'K' 
: C extends 'l' ? 'L' 
: C extends 'm' ? 'M' 
: C extends 'n' ? 'N' 
: C extends 'o' ? 'O' 
: C extends 'p' ? 'P' 
: C extends 'q' ? 'Q' 
: C extends 'r' ? 'R' 
: C extends 's' ? 'S' 
: C extends 't' ? 'T' 
: C extends 'u' ? 'U' 
: C extends 'v' ? 'V' 
: C extends 'w' ? 'W' 
: C extends 'x' ? 'X' 
: C extends 'y' ? 'Y' 
: C extends 'z' ? 'Z' 
: C
type MyCapitalize<S extends string> = S extends `${infer First}${infer Rest}` ? `${Capitalize<First>}${Rest}` : S;

Solution by eunsukimme #35137

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

Solution by raeyoung-kim #35031

Use Uppercase type to invert a lowercase letter.

// your answers
type MyCapitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : S;

Solution by dev-jaemin #34977

// 你的答案
interface LetterMap {
  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 MyCapitalize<S extends string> = S extends `${infer First}${infer Rest}`
  ? First extends keyof LetterMap
    ? `${LetterMap[First]}${Rest}`
    : S
  : S;

it doesn't need use Uppercase,i think its OK.

Solution by shx123qwe #34943

type MyCapitalize<S extends string> = S extends `${infer FirstLetter}${infer Rest}` ? `${Uppercase<FirstLetter>}${Rest}`: S

Solution by devshinthant #34573

// 你的答案

type MyCapitalize = S extends${infer A}${infer B}?${Uppercase<A>}${B}:""

Solution by W-fitTiger #34262

// 你的答案

type MyCapitalize = S extends${infer A}${infer B}?${Uppercase<A>}${B}:""

Solution by W-fitTiger #34261

// 你的答案

type MyCapitalize = S extends${infer A}${infer B}?${Uppercase<A>}${B}:""

Solution by W-fitTiger #34260

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

Solution by ouzexi #33996


type MyCapitalize<T extends string> = T extends `${infer F}${infer Rest}` ? `${Uppercase<F>}${Rest}` : T

Solution by Danny101201 #33846

I found the previous solutions were not support multi words, although it's requirement, I still wonder how to implement multi-word version, and hope this solution can help somebody

type CapitalWord<T extends string> = T extends `${infer Char}${infer Rest}`
  ? `${Uppercase<Char>}${Rest}`
  : T;

type MyCapitalize<T extends string> = T extends `${infer Word1} ${infer Word2}`
  ? `${CapitalWord<Word1>} ${MyCapitalize<Word2>}`
  : CapitalWord<T>;

Solution by pfan8 #33793

type MyCapitalize<S extends string> = S extends `${infer A}${infer Rest}`
  ? `${Uppercase<A>}${Rest}` : S

Solution by laplace1009 #33780

type AlphabetMap = {
    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 Capitalize<T extends string> =
    T extends `${infer C extends keyof AlphabetMap}${infer V}`
        ? `${AlphabetMap[C]}${V}`
        : T;

Solution by anovicenko74 #33728

// 여기 풀이를 입력하세요

type CharMap = {
  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 MyUpperCase<T extends string> = T extends keyof CharMap ? CharMap[T] : T;

type MyCapitalize<S extends string> = S extends `${infer U}${infer Rest}`
  ? `${MyUpperCase<U>}${Rest}`
  : S;

Solution by awesomelon #33433

type MyCapitalize<S extends string> = S extends `${infer Start}${infer word}` ? `${Uppercase<Start>}${word}` : S

Solution by loevray #33327

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

Solution by Tubring25 #33311

type LowerToUpper = {
  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 MyCapitalize<S extends string> =
  S extends `${infer FirstLatter extends keyof LowerToUpper}${infer Rest}`
    ? `${LowerToUpper[FirstLatter]}${Rest}`
    : S;

I guess the second extends is a condition (instead of a question like the first extends)

Solution by stavalfi #33263

// your answers
type MyCapitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : S

Solution by KeithChou #33194

// your answers

type MyCapitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Capitalize<F>}${R}` : S

Solution by KeithChou #33193

// 解答をここに記入
type MyCapitalize<S extends string> = S extends `${infer A}${infer B}`
  ? `${Uppercase<A>}${B}`
  : S;
  • 型変数Sに対して実施している判定処理はTrim, TrimLeftと同じ様なもので、文字列の1文字目をinfer Aそれ以降をinfer Bとして定義する。
  • 文字列の1文字目のみをUppercaseを使用して大文字へ変換する。

Solution by Yasunori-aloha #32902

type MyCapitalize<S extends string> = S extends `${infer F}${infer Rest}` ? `${Uppercase<F>}${Rest}` : '';

Solution by ZhipengYang0605 #32818

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

Solution by malyanoff #32782

type MyCapitalize<S extends string> = S extends `${infer I}${infer Rest}`? `${Uppercase<I>}${Rest}`  :""

Solution by bananana0118 #32767

type CapitalizeV2<T extends string> = T extends`${infer FirstLetter extends string}${infer End}`
    ? `${Uppercase<FirstLetter>}${End}`
    : never

Solution by ZhulinskiiDanil #32677

// your answers

Solution by jiak-123 #32550

type MyCapitalize<S extends string> = S extends `${infer First}${infer Rest}` ? `${Uppercase<First>}${Rest}` : S

Solution by dev-hobin #32374

type Capitalize<S extends string> = S extends `${infer x}${infer tail}` ? `${Uppercase<x>}${tail}` : S;

Solution by rkamely #32058

// your answers
type MyCapitalize<S extends string> = S extends `${infer A}${infer B}`
  ? `${Capitalize<A>}${B}`
  : "";

Solution by pea-sys #31993

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

Solution by kai-phan #31627