-
Notifications
You must be signed in to change notification settings - Fork 391
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
Comments
运用以前的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" |
// 实现 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 |
|
// 先匹配${infer U}${From}{infer U}
type R0 = Replace<'', '', ''> // '' |
1 similar comment
// 先匹配${infer U}${From}{infer U}
type R0 = Replace<'', '', ''> // '' |
// 在上一个Replace的基础上递归 type R0 = ReplaceAll<'', '', ''> // '' |
// 实现 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 |
|
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" |
实现
Replace
工具类型,用于实现字符串类型的替换操作。具体的使用示例如下所示:此外,继续实现
ReplaceAll
工具类型,用于实现替换所有满足条件的子串。具体的使用示例如下所示:The text was updated successfully, but these errors were encountered: