type TrimLeft<T extends string> = T extends ` ${infer Rest}`
? TrimLeft<Rest>
: T
type TrimRight<T extends string> = T extends `${infer Rest} `
? TrimRight<Rest>
: T
type Trim<T extends string> = TrimRight<TrimLeft<T>>
Solution by Flavour86 #33458
type Space = ' ' | '\t' | '\n';
type Trim<S extends string> = S extends `${Space}${infer word}` | `${infer word}${Space}` ? Trim<word> : S
Solution by loevray #33309
// your answers
type TrimLeft<S extends string> = S extends `${' ' | '\n' | '\t'}${infer R}` ? TrimLeft<R> : S
type TrimRight<S extends string> = S extends `${infer F}${' ' | '\n' | '\t'}` ? TrimRight<F> : S
type Trim<S extends string> = TrimLeft<TrimRight<S>>
Solution by KeithChou #33192
// 解答をここに記入
type Space = ' ' | '\n' | '\t';
type Trim<S extends string> = S extends `${Space}${infer A}`
? Trim<A>
: S extends `${infer B}${Space}`
? Trim<B>
: S;
TrimLeft
の時と同じ。Solution by Yasunori-aloha #32901
type Space = ' ' | '\n' | '\t'
type Trim<S extends string> = S extends `${Space}${infer Rest}`
? Trim<Rest>
: S extends `${infer First}${Space}` ? Trim<First> : S;
Solution by ZhipengYang0605 #32817
type Space = ' ' | '\n' | '\t'
type Trim<T extends string> = T extends `${infer S}${Space}`
? Trim<S>
: T extends `${Space}${infer S}`
? Trim<S>
: T
Solution by ZhulinskiiDanil #32676
type EmptyStrings = ' ' | '\n' | '\t';
type Trim<S extends string> = S extends `${EmptyStrings}${infer Sub}${EmptyStrings}` | `${EmptyStrings}${infer Sub}` | `${infer Sub}${EmptyStrings}`
? Trim<Sub>
: S
Solution by keyurparalkar #32467
처음 풀이
type Trim<S extends string> =
S extends `${' ' | '\n' | '\t'}${infer RR}`
? Trim<RR>
: S extends `${infer LR}${' ' | '\n' | '\t'}`
? Trim<LR>
: S
더 간결한 풀이
type Space = ' ' | '\n' | '\t'
type Trim<S extends string> =
S extends `${Space}${infer R}` | `${infer R}${Space}`
? Trim<R>
: S
Solution by dev-hobin #32365
// your answers
type Trim<S extends string> = S extends `${' ' | '\n' | '\t'}${infer U}` | `${infer U}${' ' | '\n' | '\t'}` ? Trim<U> : S
Solution by pea-sys #31970
type Space = ' ' | '\n' |'\t';
type Trim<S extends string> = S extends `${Space}${infer R}` | `${Space}${infer R}${Space}` | `${infer R}${Space}`
? Trim<R>
: S;
Solution by kai-phan #31626
type Space = '\n' | '\t' | ' '
type Trim<S extends string> = S extends `${Space}${infer A}${Space}` ? Trim<A> : S extends `${Space}${infer B}` ? Trim<B> : S extends `${infer C}${Space}` ? Trim<C> : S
Solution by jinyoung234 #31584
// your answers
type Trim<T extends string> = T extends ` ${infer Text}` | `${infer Text} ` ? Trim<Text> : T;
Solution by AhmedRagabKamal #31497
type TrimLeft<T extends string> = T extends ` ${infer P}` ? TrimLeft<P> : T;
type Trim<T extends string> = T extends ` ${infer P} ` ? Trim<P> : T;
Solution by stormt #31457
type TrimLeft<T extends string> = T extends ` ${infer P}` ? TrimLeft<P> : T;
Solution by stormt #31456
// 你的答案
Solution by LuciusXy #31442
type Space = " " | "\n" | "\t";
type Trim<S extends string> = S extends
| `${Space}${infer R}`
| `${infer R}${Space}`
? Trim<R>
: S;
Solution by vipulpathak113 #31419
type Empty = `\n`|` ` | `\t`
type TrimLeft<S extends string> = S extends `${Empty}${infer A}` ? TrimLeft<A> : S;
type TrimRight<S extends string> = S extends `${infer Rest}${infer A}${Empty}` ? TrimRight<`${Rest}${A}`> : S;
type Trim<S extends string> = TrimRight<TrimLeft<S>>
Solution by eward957 #31229
type whitespace = ' ' | '\n' | '\t'
type LeftTrim<S extends string> = S extends `${whitespace}${infer R}` ? LeftTrim<R> : S
type RigthTrim<S extends string> = S extends `${infer R}${whitespace}` ? RigthTrim<R> : S
type Trim<S extends string> = RigthTrim<LeftTrim<S>>
Solution by bicochan #31086
type Space = ' ' | '\s' | '\n';
type Trim<S extends string> = S extends `${Space}${infer A}`
? S extends `${infer B}${Space}`
? Trim<B>
: Trim<A>
: S;
Solution by ricky-simple #31074
type Space=' ' | '\t' | '\n'
type Trim<S extends string> = S extends `${infer R}${Space}`|`${Space}${infer R}`?Trim<R>:S
Solution by sw503544488 #31006
type Space = ' ' | '\s' | '\n';
type Trim<S extends string> = S extends `${Space}${infer A}`
? S extends `${infer B}${Space}`
? Trim<B>
: Trim<A>
: S;
Solution by ricky-fn #30859
type TrimLeft<S extends string> =
S extends `${infer Left}${infer Rest}`
? Left extends ' ' | '\n' | '\t'
? Trim<Rest>
: S
: ''
type Reverse<S extends string> =
S extends `${infer Left}${infer Rest}`
? Rest extends ''
? Left
: `${Reverse<Rest>}${Left}`
: ''
type Trim<S extends string> = Reverse<TrimLeft<Reverse<TrimLeft<S>>>>
Here is the great answer from issues:
type Space = ' ' | '\t' | '\n';
type Trim<S extends string> = S extends `${Space}${infer T}` | `${infer T}${Space}` ? Trim<T> : S;
Solution by GodAraden #30665
type Trim<T extends string> = T extends `${" "}${infer U}${" "}` ? Trim<U> : T;
Solution by lanshuai1208 #30207
type SpacePat = ' ' | '\n' | '\t';
type Trim<S extends string> = S extends `${SpacePat }${infer T}` | `${infer T}${SpacePat }` ? Trim<T> : S;
Solution by kanishev #30130
type Space = '\n' | ' ' | '\t';
type Trim<T extends string> = T extends `${Space}${infer R}` | `${infer R}${Space}`? Trim<R>: T;
type trimmed = Trim<' Hello World '>;
Solution by rxMATTEO #30087
템플릿리터럴과 앞뒤 공백 유니온 조합
type Space = ' ' | '\t' | '\n';
type Trim<S extends string> = S extends `${Space}${infer T}` | `${infer T}${Space}` ? Trim<T> : S;
Solution by MyeonghoonNam #29761
type Trim<S extends string> = S extends `${' ' | '\n' | '\t'}${infer U}` | `${infer U}${' ' | '\n' | '\t'}` ? Trim<U> : S
Solution by MrRabbit1993 #29727
type ToTrim = ' ' | '\n\t' | '\t'
type Trim<S extends string> = S extends `${ToTrim}${infer Rest}`
? Trim<Rest>
: S extends `${infer Rest}${ToTrim}`
? Trim<Rest>
: S
Solution by agus-wesly #29686
type Space = " " | "\n" | "\t"
type TrimLeft<S extends string> = S extends `${Space}${infer R}` ? TrimLeft<R> : S
type TrimRight<S extends string> = S extends `${infer R}${Space}` ? TrimRight<R> : S
type Trim<S extends string> = TrimLeft<TrimRight<S>>
Solution by MohammadArasteh #29388
// 你的答案
type Trim = S extends ${Space}${infer s}
|${infer s}${Space}
?Trim:S
Solution by kangaroona #29333