04803-medium-trim-right

Back

// your answers
type TrimRight<S extends string> = S extends `${infer F}${' ' | '\n' | '\t'}` ? TrimRight<F> : S

Solution by pea-sys #33082

type TrimRight<S extends string> = S extends `${infer F}${' ' | '\n' | '\t'}`
  ? TrimRight<F>
  : S

Solution by ZhulinskiiDanil #32725

type TrimRight<S extends string> = S extends `${infer Sub}${' ' | '\n' | '\t'}`
  ? TrimRight<`${Sub}`> 
  : S 

Solution by keyurparalkar #32465

type TrimRight<Str extends string> = Str extends `${infer Rest}${' ' | '\n' | '\t'}` ? TrimRight<Rest> : Str

Solution by XDUyhfu #30530

type TrimRight<Str extends string> = Str extends `${infer Start}${infer End}` ? End extends ' ' ? TrimRight<Start> : Start : Str;

Solution by XDUyhfu #30529

I thought TypeScript could only infer the first character and rest, not rest and last character... Complicated my life 🤣

// your answers

type Whitespace = " " | "\n" | "\t"

type Head<S extends string, H extends string = ""> = S extends `${infer TFirst}${infer TRest}`
  ? TRest extends ''
    ? { head: H, last: TFirst }
    : Head<TRest, `${H}${TFirst}`>
  : { head: "", last: "" };

type TrimRight<S extends string> = Head<S>["last"] extends Whitespace ? TrimRight<Head<S>["head"]> : S;

Solution by olalonde #30259

type Space = ' ' | '\n' | '\t' | '\r'

type STA<S extends string, R extends string[] = []> = 
  S extends `${infer A}${infer Rest}`
    ? 
      STA<Rest, [...R, A]>
    : R

type ATS<T extends string[], S extends string = ''> =
  T extends [infer A extends string, ...infer Rest extends string[]]
    ? 
      ATS<Rest, `${S}${A}`>
    : S

type TrimRight<S extends string, R extends string[] = STA<S>, T extends string[] = STA<S>> =
  R extends [...infer A extends string[], infer B extends string]
    ?
      B extends Space
        ?
          TrimRight<S, A, A>
        : ATS<T>
    : ATS<T>

Solution by zhangqiangzgz #30197

type Space = '\n' | ' ' | '\t';

type Trim<T extends string> = T extends `${infer R}${Space}`? Trim<R>: T;

type trimmed = Trim<'  Hello World  '>;

Solution by rxMATTEO #30088

type Space = " " | "\n" | "\t";
type TrimRight<S extends string> = S extends `${infer R}${Space}` ? TrimRight<R> : S

Solution by MohammadArasteh #29387

type Space = " " | "\n" | "\t"

type TrimRight<S extends string> = S extends `${infer Head}${Space}`
  ? `${TrimRight<Head>}`
  : S

Solution by Kying-star #29038

type Space = " " | "\n" | "\t";
type TrimRight<S extends string> = S extends `${infer Str}${Space}`
  ? TrimRight<Str>
  : S;

Solution by DoubleWoodLin #28776

type Space = ' ' | '\n' | '\t'
type TrimRight<T> = T extends `${infer Pre}${Space}` ? TrimRight<Pre> : T

Solution by jiechliu #27717

type TrimRight<S extends string> = S extends `${infer Left}${' ' | '\n' | '\t'}` ? TrimRight<Left> : S

Solution by jjswifty #27469

type Space = ' ' | '\n' | '\t';

type TrimRight<S extends string> = S extends `${infer F}${Space}` ? TrimRight<F> : S;

Solution by RainbowIsPerfect #27296

type TrimRight<T extends string> = T extends `${infer R} ` ? TrimRight<R> : T;

Solution by ryuji-1to #27263

type Space = ' ' | '\t' | '\n';
type TrimRight<S extends string> = S extends `${infer R}${Space}` ? TrimRight<R> : S;

Solution by smileboyi #27103

// 你的答案
type TrimRight<S extends string> = S extends `${infer X}${' '|'\n'|'\t'}` ? TrimRight<X> : S

Solution by kiki-zjq #25665

type TrimRight<S extends string> = S extends `${infer Left} ` | `${infer Left}\n` | `${infer Left}\t` ? TrimRight<Left> : S;

Solution by kakasoo #25416

type whiteSpace = ' ' | '\t' | '\n';
type TrimRight<S extends string> = S extends `${infer W}${whiteSpace}` ? TrimRight<W> : S

Solution by Lu9709 #24769

type TrimRight<S extends string> = S extends `${infer Start}${' ' | '\n\t'}`
? TrimRight<Start>
: S

Solution by NeylonR #24269

// your answers
type TrimRight<S extends string> = S extends `${infer Rest}${'\n' | '\t' | ' '}` ? TrimRight<Rest> : S

Solution by studymachiney #24077

type TrimRight<S extends string> = S extends `${infer R}${` ` | `\n` | `\t`}` ? TrimRight<F> : S;

// old way
// type TrimRight<S extends string> = S extends `${infer R}${` `|`\n`|`\t`|`\n\t`|`\t\n`}` ? TrimRight<R> : S;

Solution by E-uler #23972

type Whitespace = ' ' | '\n' | '\t';
type TrimRight<S extends string> = S extends `${infer U}${Whitespace}` ? TrimRight<U> : S;

Solution by mrdulin #23708

// your answers
/**
 * A type that matches any whitespace.
 */
type Whitespace = 
  "\t"
  | "\v"
  | "\f"
  | " "
  | "\u00A0"
  | "\uFEFF"
  | "\u0020"
  | "\u3000"
  | "\u1680"
  | "\u2000"
  | "\u2006"
  | "\u2008"
  | "\u200A"
  | "\u205F"
  | "\u00A0"
  | "\u2007"
  | "\u202F";

/**
 * A type that matches any line terminator.
 */
type LineTerminator = 
  "\n"
  | "\r"
  | "\u2028"
  | "\u2029";

/**
 * A type that matches any whitespace and line terminator.
 */
type WhitespaceOrLineTerminator = Whitespace | LineTerminator;

/**
 * Removes whitespaces from the beginning of the string.
 * @template Str - The string from which to remove whitespaces.
 */
type TrimRight<Str extends string> = 
  Str extends `${infer Rest}${WhitespaceOrLineTerminator}`
    ? TrimEnd<Rest>
    : Str;

Solution by jakub791 #23295

type TrimRight<S extends string, K extends string = ' ' | '\n' | '\t'> = S extends `${infer R}${K}` ? TrimRight<R> : S

Solution by asurewall #23138

type Blank = ' ' | '\n\t'
type TrimRight<S extends string> = S extends `${infer A}${Blank}`? TrimRight<A> : S

Solution by snakeUni #23112

type Space = " " | "\n" | "\t";
type TrimRight<S extends string> = S extends `${infer R}${Space}`
  ? TrimRight<R>
  : S;

Solution by coderyoo1 #22881

// 你的答案
type TrimRight<S extends string> = S extends `${infer Str}${' ' | '\n' | '\t'}` ? TrimRight<Str> : S;

Solution by jxhhdx #22729

type TrimRight<S extends string> = S extends `${infer F}${" " | "\n" | "\t"}`
  ? TrimRight<F>
  : S;

Solution by BarrySong97 #21812

type Spaces = ' ' | '\n' | '\t';

type ReverseString<S extends string = ''> = S extends `${infer F}${infer R}` ? `${ReverseString<R>}${F}` : S;

type TrimLeft<S extends string> = S extends `${Spaces}${infer R}` ? `${TrimLeft<R>}` : S;

type TrimRight<S extends string> = ReverseString<TrimLeft<ReverseString<S>>>

Solution by rin-st #21128