ReplaceAll
介绍
实现 ReplaceAll<S, From, To>
将一个字符串 S
中的所有子字符串 From
替换为 To
。
例如
ts
type replaced = ReplaceAll<'t y p e s', ' ', ''> // 期望是 'types'
View on GitHubts
type replaced = ReplaceAll<'t y p e s', ' ', ''> // 期望是 'types'
起点
ts
/* _____________ Your Code Here _____________ */typeReplaceAll <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 <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 <'', '', ''>, ''>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeReplaceAll <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 <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 <'', '', ''>, ''>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
//most populartypeReplaceAll <S extends string,From extends string,To extends string> =S extends ''?S :S extends `${inferF }${From }${inferRest }`? `${F }${To }${ReplaceAll <Rest ,From ,To >}`:S ;
ts
//most populartypeReplaceAll <S extends string,From extends string,To extends string> =S extends ''?S :S extends `${inferF }${From }${inferRest }`? `${F }${To }${ReplaceAll <Rest ,From ,To >}`:S ;
ts
// 存在问题typereplace <S extends string,K extends string,To extends string> =K extends ''?S :S extends `${inferR }${K }${inferRest }`? `${R }${To }${Rest }`:S typeReplaceAll <S extends string,K extends string,To extends string> =K extends ''?S :S extends `${inferR }${K }${inferRest }`?ReplaceAll <replace <S ,K ,To >,K ,To >:S ;
ts
// 存在问题typereplace <S extends string,K extends string,To extends string> =K extends ''?S :S extends `${inferR }${K }${inferRest }`? `${R }${To }${Rest }`:S typeReplaceAll <S extends string,K extends string,To extends string> =K extends ''?S :S extends `${inferR }${K }${inferRest }`?ReplaceAll <replace <S ,K ,To >,K ,To >:S ;