-
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 练习题第四十八题 #69
Comments
type ConsistsOnlyOf<LongString extends string, Substring extends string> = LongString extends ''
? true
: LongString extends `${Substring}${infer B}`
? ConsistsOnlyOf<B, Substring>
: false; 思路: 字符串匹配,利用 extends 、利用ts下支持的模板字符串语法,利用 infer 从开头一步步的去匹配,减治的思想。 |
type ConsistsOnlyOf<
LongString extends string,
Substring extends string
> = LongString extends `${Substring}${infer R}`
? ConsistsOnlyOf<R, Substring>
: LongString extends ""
? true
: false; |
type ConsistsOnlyOf<LongString extends string, Substring extends string> =
LongString extends ''
? true
: LongString extends `${Substring}${infer S}`
? ConsistsOnlyOf<S, Substring>
: false |
type ConsistsOnlyOf<LongString extends string, Substring extends string>
= LongString extends `${Substring}${infer Others}`
? ConsistsOnlyOf<Others, Substring>
: LongString extends '' ? true : false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
实现
ConsistsOnlyOf
工具类型,用于判断LongString
字符串类型是否由 0 个或多个Substring
字符串类型组成。具体的使用示例如下所示:The text was updated successfully, but these errors were encountered: