Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

「重学TS 2.0 」TS 练习题第四十一题 #62

Open
semlinker opened this issue Sep 30, 2021 · 9 comments
Open

「重学TS 2.0 」TS 练习题第四十一题 #62

semlinker opened this issue Sep 30, 2021 · 9 comments

Comments

@semlinker
Copy link
Owner

实现 Replace 工具类型,用于实现字符串类型的替换操作。具体的使用示例如下所示:

type Replace<
  S extends string,
  From extends string,
  To extends string
> = // 你的实现代码 
  
type R0 = Replace<'', '', ''> // ''
type R1 = Replace<'foobar', 'bar', 'foo'> // "foofoo"
type R2 = Replace<'foobarbar', 'bar', 'foo'> // "foofoobar"

此外,继续实现 ReplaceAll 工具类型,用于实现替换所有满足条件的子串。具体的使用示例如下所示:

type ReplaceAll<
  S extends string,
  From extends string,
  To extends string
> = // 你的实现代码 

type R0 = ReplaceAll<'', '', ''> // ''
type R1 = ReplaceAll<'barfoo', 'bar', 'foo'> // "foofoo"
type R2 = ReplaceAll<'foobarbar', 'bar', 'foo'> // "foofoofoo"
type R3 = ReplaceAll<'foobarfoobar', 'ob', 'b'> // "fobarfobar"

请在下面评论你的答案

@sunboyZgz
Copy link

运用以前的Split和JoinStrArray

type Replace<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer H}${From}${infer R}` ? `${H}${To}${R}` : S;

type ReplaceAll<
  S extends string,
  From extends string,
  To extends string
> = JoinStrArray<Split<S, From>, To>;
type R01 = Replace<"", "", "">; // ''
type R11 = Replace<"foobar", "bar", "foo">; // "foofoo"
type R21 = Replace<"foobarbar", "bar", "foo">; // "foofoobar"
type R3 = ReplaceAll<"foobarfoobarob", "ob", "b">; // "fobarfobar"

@zhaoxiongfei
Copy link

zhaoxiongfei commented Sep 30, 2021

// 实现 Replace 工具类型,用于实现字符串类型的替换操作。具体的使用示例如下所示:

type Replace<
  S extends string,
  From extends string,
  To extends string,
> = S extends `${infer H}${From}${infer T}` ? `${H}${To}${T}` : S;

type R0 = Replace<"", "", "">; // ''
type R1 = Replace<"foobar", "bar", "foo">; // "foofoo"
type R2 = Replace<"foobarbar", "bar", "foo">; // "foofoobar"
// 此外,继续实现 ReplaceAll 工具类型,用于实现替换所有满足条件的子串。具体的使用示例如下所示:

type ReplaceAll<
  S extends string,
  From extends string,
  To extends string,
> = S extends `${infer H}${From}${infer T}`
  ? `${ReplaceAll<H, From, To>}${To}${ReplaceAll<T, From, To>}`
  : S;

type R0 = ReplaceAll<"", "", "">; // ''
type R1 = ReplaceAll<"barfoo", "bar", "foo">; // "foofoo"
type R2 = ReplaceAll<"foobarbar", "bar", "foo">; // "foofoofoo"
type R3 = ReplaceAll<"foobarfoobar", "ob", "b">; // "fobarfobar"

思路: 利用extends 配合infer 配合字符串模板变量的写法就能提取出指定的子字符串,之后利用递归就可以ReplaceAll

@mingzhans
Copy link

type Replace<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer A}${From}${infer B}` ? `${A}${To}${B}` : S// 你的实现代码 
  
type R0 = Replace<'', '', ''> // ''
type R1 = Replace<'foobar', 'bar', 'foo'> // "foofoo"
type R2 = Replace<'foobarbar', 'bar', 'foo'> // "foofoobar"

type ReplaceAll<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer A}${From}${infer Rest}` ? `${A}${To}${ReplaceAll<Rest,From,To>}` : S// 你的实现代码 
  
type C0 = ReplaceAll<'', '', ''> // ''
type C1 = ReplaceAll<'foobar', 'bar', 'foo'> // "foofoo"
type C2 = ReplaceAll<'foobarbar', 'bar', 'foo'> // "foofoobar"
type C3 = ReplaceAll<'foobarfoobar', 'ob', 'b'> // "fobarfobar"

@jackwangwj
Copy link

// 先匹配${infer U}${From}{infer U}
// 然后替换
type Replace<
S extends string,
From extends string,
To extends string

= S extends ${infer Head}${From}${infer Tail} ?
${Head}${To}${Tail} : S
// 你的实现代码

type R0 = Replace<'', '', ''> // ''
type R1 = Replace<'foobar', 'bar', 'foo'> // "foofoo"
type R2 = Replace<'foobarbar', 'bar', 'foo'> // "foofoobar"

1 similar comment
@jackwangwj
Copy link

// 先匹配${infer U}${From}{infer U}
// 然后替换
type Replace<
S extends string,
From extends string,
To extends string

= S extends ${infer Head}${From}${infer Tail} ?
${Head}${To}${Tail} : S
// 你的实现代码

type R0 = Replace<'', '', ''> // ''
type R1 = Replace<'foobar', 'bar', 'foo'> // "foofoo"
type R2 = Replace<'foobarbar', 'bar', 'foo'> // "foofoobar"

@jackwangwj
Copy link

// 在上一个Replace的基础上递归
// 此题有个陷阱,R3用例中"fobarfobar",进一步替换可以变为"fbarfbar",
// 所以得处理下,Head部分和替换部分不参与递归
type ReplaceAll<
S extends string,
From extends string,
To extends string
> = S extends ${infer Head}${From}${infer Tail}
? ${Head}${To}${ReplaceAll<Tail, From, To>}
: S

type R0 = ReplaceAll<'', '', ''> // ''
type R1 = ReplaceAll<'barfoo', 'bar', 'foo'> // "foofoo"
type R2 = ReplaceAll<'foobarbar', 'bar', 'foo'> // "foofoofoo"
type R3 = ReplaceAll<'foobarfoobar', 'ob', 'b'> // "fobarfobar"

@ChangerHe
Copy link

// 实现 Replace 工具类型,用于实现字符串类型的替换操作

// 解法: 通过模板字符串结合infer即可完成替换
type Replace<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer F}${From}${infer L}` ? `${F}${To}${L}` : S

// 继续实现 ReplaceAll 工具类型,用于实现替换所有满足条件的子串

// 解法: 在replace的基础上, 递归对前后字符串进行处理即可
type ReplaceAll<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer F}${From}${infer L}` ? `${ReplaceAll<F, From, To>}${To}${ReplaceAll<L, From, To>}` : S

@ChuTingzj
Copy link

// 实现 Replace 工具类型,用于实现字符串类型的替换操作。具体的使用示例如下所示:
// type Replace<
//   S extends string,
//   From extends string,
//   To extends string
// > = // 你的实现代码
// type R0 = Replace<'', '', ''> // ''
// type R1 = Replace<'foobar', 'bar', 'foo'> // "foofoo"
// type R2 = Replace<'foobarbar', 'bar', 'foo'> // "foofoobar"

type Replace<S extends string, From extends string, To extends string> = S extends `${From}${infer Rest}`
  ? `${To}${Rest}`
  : S extends `${infer Head}${From}${infer Rest}`
  ? `${Head}${To}${Rest}`
  : S extends `${infer Head}${infer From}`
  ? `${Head}${To}`
  : S
type R0 = Replace<'', '', ''> // ''
type R11 = Replace<'foobar', 'bar', 'foo'> // "foofoo"
type R2 = Replace<'foobarbar', 'bar', 'foo'> // "foofoobar"

// 此外,继续实现 ReplaceAll 工具类型,用于实现替换所有满足条件的子串。具体的使用示例如下所示:
// type ReplaceAll<
//   S extends string,
//   From extends string,
//   To extends string
// > = // 你的实现代码
// type R0 = ReplaceAll<'', '', ''> // ''
// type R1 = ReplaceAll<'barfoo', 'bar', 'foo'> // "foofoo"
// type R2 = ReplaceAll<'foobarbar', 'bar', 'foo'> // "foofoofoo"
// type R3 = ReplaceAll<'foobarfoobar', 'ob', 'b'> // "fobarfobar"

type ReplaceAll<S extends string, From extends string, To extends string> = S extends `${From}`
  ? S
  : S extends `${From}${infer Rest}`
  ? `${To}${ReplaceAll<Rest, From, To>}`
  : S extends `${infer Head}${From}${infer Rest}`
  ? `${Head}${To}${ReplaceAll<Rest, From, To>}`
  : S extends `${infer Head}${infer From}`
  ? `${Head}${To}`
  : S
type R00 = ReplaceAll<'', '', ''> // ''
type R111 = ReplaceAll<'barfoo', 'bar', 'foo'> // "foofoo"
type R22 = ReplaceAll<'foobarbar', 'bar', 'foo'> // "foofoofoo"
type R33 = ReplaceAll<'foobarfoobar', 'ob', 'b'> // "fobarfobar"

@dolphin0618
Copy link

dolphin0618 commented Jul 26, 2022

type Replace<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer A}${From}${infer B}` ? `${A}${To}${B}` : S

type ReplaceAll<
  S extends string,
  From extends string,
  To extends string
> = S extends `${infer A}${From}${infer B}` ? `${A}${To}${ReplaceAll<B, From, To>}` : S
  
type R0 = ReplaceAll<'', '', ''> // ''
type R1 = ReplaceAll<'barfoo', 'bar', 'foo'> // "foofoo"
type R2 = ReplaceAll<'foobarbar', 'bar', 'foo'> // "foofoofoo"
type R3 = ReplaceAll<'foobarfoobar', 'ob', 'b'> // "fobarfobar"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants