跳到主要内容

ReplaceAll

介绍

实现 ReplaceAll<S, From, To> 将一个字符串 S 中的所有子字符串 From 替换为 To

例如

ts
type replaced = ReplaceAll<'t y p e s', ' ', ''> // 期望是 'types'
ts
type replaced = ReplaceAll<'t y p e s', ' ', ''> // 期望是 'types'
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type ReplaceAll<S extends string, From extends string, To extends string> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<ReplaceAll<'foobar', 'bar', 'foo'>, 'foofoo'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'foobar', 'bag', 'foo'>, 'foobar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'foobarbar', 'bar', 'foo'>, 'foofoofoo'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'t y p e s', ' ', ''>, 'types'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'foobarbar', '', 'foo'>, 'foobarbar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'barfoo', 'bar', 'foo'>, 'foofoo'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'foobarfoobar', 'ob', 'b'>, 'fobarfobar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'foboorfoboar', 'bo', 'b'>, 'foborfobar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'', '', ''>, ''>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
ts
/* _____________ Your Code Here _____________ */
 
type ReplaceAll<S extends string, From extends string, To extends string> = any
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<ReplaceAll<'foobar', 'bar', 'foo'>, 'foofoo'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'foobar', 'bag', 'foo'>, 'foobar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'foobarbar', 'bar', 'foo'>, 'foofoofoo'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'t y p e s', ' ', ''>, 'types'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'foobarbar', '', 'foo'>, 'foobarbar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'barfoo', 'bar', 'foo'>, 'foofoo'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'foobarfoobar', 'ob', 'b'>, 'fobarfobar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'foboorfoboar', 'bo', 'b'>, 'foborfobar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<ReplaceAll<'', '', ''>, ''>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
//most popular
 
type ReplaceAll<S extends string, From extends string, To extends string> = S extends ''
? S
: S extends `${infer F}${From}${infer Rest}`
? `${F}${To}${ReplaceAll<Rest, From, To>}`
: S;
ts
//most popular
 
type ReplaceAll<S extends string, From extends string, To extends string> = S extends ''
? S
: S extends `${infer F}${From}${infer Rest}`
? `${F}${To}${ReplaceAll<Rest, From, To>}`
: S;
ts
// 存在问题
 
type replace<S extends string, K extends string, To extends string> = K extends ''
? S
: S extends `${infer R}${K}${infer Rest}`
? `${R}${To}${Rest}`
: S
 
 
type ReplaceAll<S extends string, K extends string, To extends string> = K extends ''
? S
: S extends `${infer R}${K}${infer Rest}`
? ReplaceAll<replace<S, K, To>, K, To>
: S;
 
ts
// 存在问题
 
type replace<S extends string, K extends string, To extends string> = K extends ''
? S
: S extends `${infer R}${K}${infer Rest}`
? `${R}${To}${Rest}`
: S
 
 
type ReplaceAll<S extends string, K extends string, To extends string> = K extends ''
? S
: S extends `${infer R}${K}${infer Rest}`
? ReplaceAll<replace<S, K, To>, K, To>
: S;
 
view more solutions