type Space = '\n' | '\t' | ' ';
type TrimRight<S extends string> = S extends `${infer Rest}${Space}` ? TrimRight<Rest> : S;
Solution by wendao-liu #35217
type TrimRight<S extends string> = S extends `${infer R}${' ' | '\n' | '\t'}` ? TrimRight<R> : S
Solution by ouzexi #34075
type TrimRight<S extends string> = S extends `${infer rest}${' ' | '\n' | '\t'}` ? TrimRight<rest> : S
Solution by ouzexi #33994
by Yugang Cao (@Talljack) #medium #template-literal
Implement TrimRight<T>
which takes an exact string type and returns a new string with the whitespace ending removed.
For example:
type Trimed = TrimRight<' Hello World '> // expected to be ' Hello World'
View on GitHub: https://tsch.js.org/4803
...
type Reverse<S extends string> = S extends `${infer L}${infer R}`
? `${Reverse<R>}${L}`
: S
type WS = ' '|'\n'|'\t'
type Trim<S extends string> = S extends `${WS}${infer R}` ? Trim<R> : S
type TrimRight<S extends string> = Reverse<Trim<Reverse<S>>>
type TrimRight1<S extends string> =
S extends `${infer Left}${WS}` ? (
TrimRight<Left>
) : S
Solution by veralex #33839
// 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