Replace
介绍
实现 Replace<S, From, To>
将字符串 S
中的第一个子字符串 From
替换为 To
。
例如
ts
type replaced = Replace<'types are fun!', 'fun', 'awesome'> // 期望是 'types are awesome!'
View on GitHubts
type replaced = Replace<'types are fun!', 'fun', 'awesome'> // 期望是 'types are awesome!'
起点
ts
/* _____________ Your Code Here _____________ */typeReplace <S extends string,From extends string,To extends string> = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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 <'', '', ''>, ''>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeReplace <S extends string,From extends string,To extends string> = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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 <'', '', ''>, ''>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
//方法1// 这里要注意的: S extends `${infer F}${infer Rest}` F的代表的区域.typeReplace <S extends string,From extends string,To extends string> =From extends ''?S :S extends `${inferF }${From }${inferRest }`? `${F }${To }${Rest }`:S
ts
//方法1// 这里要注意的: S extends `${infer F}${infer Rest}` F的代表的区域.typeReplace <S extends string,From extends string,To extends string> =From extends ''?S :S extends `${inferF }${From }${inferRest }`? `${F }${To }${Rest }`:S