跳到主要内容

Replace

介绍

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

例如

ts
type replaced = Replace<'types are fun!', 'fun', 'awesome'> // 期望是 'types are awesome!'
ts
type replaced = Replace<'types are fun!', 'fun', 'awesome'> // 期望是 'types are awesome!'
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type Replace<S extends string, From extends string, To extends string> = any
 
 
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<Replace<'foobar', 'bar', 'foo'>, 'foofoo'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Replace<'foobarbar', 'bar', 'foo'>, 'foofoobar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Replace<'foobarbar', '', 'foo'>, 'foobarbar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Replace<'foobarbar', 'bar', ''>, 'foobar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Replace<'foobarbar', 'bra', 'foo'>, 'foobarbar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Replace<'', '', ''>, ''>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
ts
/* _____________ Your Code Here _____________ */
 
type Replace<S extends string, From extends string, To extends string> = any
 
 
 
/* _____________ Test Cases _____________ */
 
type cases = [
Expect<Equal<Replace<'foobar', 'bar', 'foo'>, 'foofoo'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Replace<'foobarbar', 'bar', 'foo'>, 'foofoobar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Replace<'foobarbar', '', 'foo'>, 'foobarbar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Replace<'foobarbar', 'bar', ''>, 'foobar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Replace<'foobarbar', 'bra', 'foo'>, 'foobarbar'>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Replace<'', '', ''>, ''>>,
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
//方法1
// 这里要注意的: S extends `${infer F}${infer Rest}` F的代表的区域.
type Replace<S extends string, From extends string, To extends string> = From extends ''
? S
: S extends `${infer F}${From}${infer Rest}`
? `${F}${To}${Rest}`
: S
 
ts
//方法1
// 这里要注意的: S extends `${infer F}${infer Rest}` F的代表的区域.
type Replace<S extends string, From extends string, To extends string> = From extends ''
? S
: S extends `${infer F}${From}${infer Rest}`
? `${F}${To}${Rest}`
: S
 
view more solutions