type ReplaceAll<
S extends string,
From extends string,
To extends string
> = From extends ""
? S
: S extends `${infer F}${From}${infer U}`
? `${F}${To}${ReplaceAll<U, From, To>}`
: S;
Solution by LennyLee1998 #35196
type ReplaceAll<S extends string, From extends string, To extends string, ACC extends string = ''> = From extends '' ? S
: S extends `${infer Left}${From}${infer Right}` ? ReplaceAll<Right, From, To, `${ACC}${Left}${To}`>
: `${ACC}${S}`
Solution by eunsukimme #35139
type ReplaceAll<Str, From extends string, To extends string> =
Str extends `${infer R1}${From}${infer R2}` ? `${ReplaceAll<R1, From, To>}${To}${ReplaceAll<R2, From, To>}` : Str;
Solution by kyzinatra #35028
// your answers
type ReplaceAll<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer Start}${From}${infer End}` ? `${Start}${To}${ReplaceAll<End, From, To>}` : S;
Solution by LeeKangHyun #35010
We can solve easily with Replace
type and recursive left and right types.
// your answers
type ReplaceAll<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer L}${From}${infer R}` ? `${ReplaceAll<L, From, To>}${To}${ReplaceAll<R, From, To>}` : S;
Solution by dev-jaemin #35005
type ReplaceAll<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer First}${From}${infer Rest}` ? `${First}${To}${ReplaceAll<Rest,From,To>}` : S
Solution by devshinthant #34575
type ReplaceAll<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer prefix}${From}${infer suffix}` ? `${prefix}${To}${ReplaceAll<suffix, From, To>}` : S
Solution by ouzexi #33998
type ReplaceAll<S extends string, From extends string, To extends string, K extends string = ''> = S extends `${infer L}${From extends '' ? never : From}${infer R}`
? ReplaceAll<R, From, To,`${K}${L}${To}`>
: `${K}${S}`
Solution by IamSmaLLR #33920
// 你的答案
type ReplaceAll<
S extends string,
From extends string,
To extends string
> = From extends ""
? S
: S extends `${infer V}${From}${infer R}`
? `${V}${To}${ReplaceAll<`${R}`, From, To>}`
: S;
Solution by shawnliu200058 #33917
type ReplaceAll<S extends string, From extends string, To extends string> = From extends ''
? S
: S extends `${infer F}${From}${infer R}`
// 下面这样会递归替换,但是不需要处理已经替换过的字符串,也就是只需要递归处理右侧剩余的
// ? ReplaceAll<`${F}${To}${R}`, From, To>
? `${F}${To}${ReplaceAll<R, From, To>}`
: S
Solution by ScriptBloom #33896
type ReplaceAll<S extends string, T extends string, P extends string> =
T extends ''
? S
: S extends `${infer L}${T}${infer R}`
? `${ReplaceAll<L, T, P>}${P}${ReplaceAll<R, T, P>}`
: S
Solution by Danny101201 #33849
by Anthony Fu (@antfu) #medium #template-literal
Implement ReplaceAll<S, From, To>
which replace the all the substring From
with To
in the given string S
For example
type replaced = ReplaceAll<'t y p e s', ' ', ''> // expected to be 'types'
View on GitHub: https://tsch.js.org/119
...
type ReplaceAll<S extends string, From extends string, To extends string> = From extends ``
? S
: S extends `${infer L}${From}${infer R}`
? `${L}${To}${ReplaceAll<R, From, To>}`
: S
Solution by veralex #33782
type ReplaceAll<S extends string, From extends string, To extends string> = S extends ${infer F}${From}${infer E}
?
${F}${To}${ReplaceAll<E, From, To>}
:S
Solution by rookiewxy #33688
type ReplaceAll<
S extends string,
From extends string,
To extends string
> = From extends ""
? S
: S extends `${infer Start}${From}${infer End}`
? `${Start}${To}${ReplaceAll<End, From, To>}`
: S;
Solution by fyuanz #33685
type ReplaceAll<S extends string, From extends string, To extends string> = From extends ''
? S : S extends `${infer left}${From}${infer right}`
? `${ReplaceAll<left,From,To>}${To}${ReplaceAll<right,From,To>}` : S
Solution by loevray #33349
type ReplaceAll<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer F}${From}${infer L}` ? `${F}${To}${ReplaceAll<L, From, To>}` : S
Solution by Tubring25 #33313
// 你的答案
type ReplaceAll<T extends string, U , V extends string> = T extends `${infer A}${infer B}`
? A extends U
? `${V}${ReplaceAll<`${B}`,U,V>}`
: `${A}${ReplaceAll<`${B}`,U,V>}`
: T;
Solution by 2531800823 #33251
// your answers
type ReplaceAll<S extends string, From extends string, To extends string> =
From extends ''
? S
: S extends `${infer F}${From}${infer R}`
? `${F}${To}${ReplaceAll<R, From, To>}`
: S
Solution by KeithChou #33196
type ReplaceAll<S extends string, From extends string, To extends string> = From extends ""
? S
: S extends `${infer L}${From}${infer R}`
? `${ReplaceAll<L, From, To>}${To}${ReplaceAll<R, From, To>}`
: S
Solution by Taneros #33152
// 你的答案
type ReplaceAll<S extends string, From extends string, To extends string> = From extends ''?S
:S extends `${infer L}${From}${infer R}` ? `${L}${To}${ReplaceAll<R,From,To>}`
:S
Solution by walker-hzx #32938
type ReplaceAll<S extends string, From extends string, To extends string> =
From extends ''
? S
: S extends `${infer H}${From}${infer T}`
? T extends ""
? `${H}${To}`
: `${H}${To}${ReplaceAll<T, From, To>}`
: S```
Solution by keyurparalkar #32411
type ReplaceAll<S extends string, From extends string, To extends string> =
From extends ''
? S
: S extends `${infer Head}${From}${infer Tail}`
? `${Head}${To}${ReplaceAll<Tail, From, To>}`
: S
Solution by dev-hobin #32376
type ReplaceAll<S extends string, From extends string, To extends string> =
S extends `${infer A}${From extends '' ? never : From}${infer B}`
? `${A}${To}${ReplaceAll<B, From, To>}`
: S
Solution by nivenice #32372
type ReplaceAll<
S extends string,
From extends string,
To extends string,
> = S extends `${infer L}${From}${infer R}`
? S
: S;
type ReplaceAll<
S extends string,
From extends string,
To extends string,
> = S extends `${infer L}${From}${infer R}`
? `${L}${To}${R}`
: S;
type ReplaceAll<
S extends string,
From extends string,
To extends string,
> = S extends `${infer L}${From}${infer R}`
? `${L}${From extends "" ? "" : To}${R}`
: S;
type ReplaceAll<
S extends string,
From extends string,
To extends string,
> = S extends `${infer L}${From}${infer R}`
? `${L}${From extends "" ? "" : To}${ReplaceAll<`${R}`, From, To>}`
: S;
Solution by awkmia #32236
type ReplaceAll<S extends string, From extends string, To extends string> =
S extends `${infer F}${From extends '' ? never : From}${infer L}`
? `${F}${To}${ReplaceAll<L, From, To>}`
: S
We infer the first appear of From
type, replace it with To
and concatenate the string from the beginning to from with recursively ReplaceAll
of Tail
Solution by joyanedel #32155
type ReplaceAll<S extends string, From extends string, To extends string, Acc extends string = ""> = From extends ""
? S
: S extends `${Acc}${infer F}${From}${infer L}`
? L extends ""
? `${Acc}${F}${To}`
: ReplaceAll<`${Acc}${F}${To}${L}`, From, To, `${Acc}${F}${To}`>
: S;
Solution by gasmg #32060
// your answers
type ReplaceAll<S extends string, From extends string, To extends string> = From extends '' ? S : S extends `${infer F}${From}${infer R}` ? `${F}${To}${ReplaceAll<R, From, To>}` : S;
Solution by pea-sys #32020
type ReplaceAll<S extends string, From extends string, To extends string> =
From extends '' ? S :
S extends `${infer First}${From}${infer Rest}` ?
`${First}${To}${ReplaceAll<`${Rest}`, From, To>}` :
S
Solution by jinyoung234 #31702
type ReplaceAll<S extends string, From extends string, To extends string> = S extends `${infer F}${From extends '' ? never : From}${infer R}`
? `${ReplaceAll<F, From, To>}${To}${ReplaceAll<R, From, To>}`
: S
Solution by kai-phan #31629
// your answers
type ReplaceAll<
S extends string,
From extends string,
To extends string
> = S extends `${infer L}${From}${infer R}` ? `${L}${To}${ReplaceAll<R, From, To>}` : S;
Solution by AhmedRagabKamal #31509