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 练习题第三十八题 #58

Open
semlinker opened this issue Sep 25, 2021 · 5 comments
Open

「重学TS 2.0 」TS 练习题第三十八题 #58

semlinker opened this issue Sep 25, 2021 · 5 comments

Comments

@semlinker
Copy link
Owner

semlinker commented Sep 25, 2021

实现 StartsWith 工具类型,判断字符串字面量类型 T 是否以给定的字符串字面量类型 U 开头,并根据判断结果返回布尔值。具体的使用示例如下所示:

type StartsWith<T extends string, U extends string> = // 你的实现代码

type S0 = StartsWith<'123', '12'> // true
type S1 = StartsWith<'123', '13'> // false
type S2 = StartsWith<'123', '1234'> // false

此外,继续实现 EndsWith 工具类型,判断字符串字面量类型 T 是否以给定的字符串字面量类型 U 结尾,并根据判断结果返回布尔值。具体的使用示例如下所示:

type EndsWith<T extends string, U extends string> = // 你的实现代码

type E0 = EndsWith<'123', '23'> // true
type E1 = EndsWith<'123', '13'> // false
type E2 = EndsWith<'123', '123'> // true

请在下面评论你的答案

@zhaoxiongfei
Copy link

// 实现 StartsWith 工具类型,判断字符串字面量类型 T 是否以给定的字符串字面量类型 U 开头,并根据判断结果返回布尔值。具体的使用示例如下所示:
type StartsWith<T extends string, U extends string> = T extends `${U}${infer Rest}` ? true : false;

type S0 = StartsWith<"123", "12">; // true
type S1 = StartsWith<"123", "13">; // false
type S2 = StartsWith<"123", "1234">; // false

// 此外,继续实现 EndsWith 工具类型,判断字符串字面量类型 T 是否以给定的字符串字面量类型 U 结尾,并根据判断结果返回布尔值。具体的使用示例如下所示:
type EndsWith<T extends string, U extends string> = T extends `${infer Head}${U}` ? true : false;

type E0 = EndsWith<"123", "23">; // true
type E1 = EndsWith<"123", "13">; // true
type E2 = EndsWith<"123", "123">; // true

@mingzhans
Copy link

type StartsWith<T extends string, U extends string> = T extends `${U}${string}` ? true : false// 你的实现代码
type EndsWith<T extends string, U extends string> = T extends `${string}${U}` ? true : false// 你的实现代码

type S0 = StartsWith<'123', '12'> // true
type S1 = StartsWith<'123', '13'> // false
type S2 = StartsWith<'123', '1234'> // false

type E0 = EndsWith<'123', '23'> // true
type E1 = EndsWith<'123', '13'> // false
type E2 = EndsWith<'123', '123'> // true

模板字面量的使用,里面可以直接放string,number等类型

@zjxxxxxxxxx
Copy link

type StartsWith<T extends string, U extends string> = T extends `${U}${infer R}`
  ? true
  : false;

type EndsWith<T extends string, U extends string> = T extends `${infer F}${U}`
  ? true
  : false;

@ChuTingzj
Copy link

// 实现 StartsWith 工具类型,判断字符串字面量类型 T 是否以给定的字符串字面量类型 U 开头,并根据判断结果返回布尔值。具体的使用示例如下所示:
// type StartsWith<T extends string, U extends string> = // 你的实现代码
// type S0 = StartsWith<'123', '12'> // true
// type S1 = StartsWith<'123', '13'> // false
// type S2 = StartsWith<'123', '1234'> // false

type StartsWith<T extends string, U extends string> = T extends `${U}${infer Rest}` ? true : false
type S000 = StartsWith<'123', '12'> // true
type S111 = StartsWith<'123', '13'> // false
type S222 = StartsWith<'123', '1234'> // false

// 此外,继续实现 EndsWith 工具类型,判断字符串字面量类型 T 是否以给定的字符串字面量类型 U 结尾,并根据判断结果返回布尔值。具体的使用示例如下所示:
// type EndsWith<T extends string, U extends string> = // 你的实现代码
// type E0 = EndsWith<'123', '23'> // true
// type E1 = EndsWith<'123', '13'> // false
// type E2 = EndsWith<'123', '123'> // true

type EndsWith<T extends string, U extends string> = T extends `${infer Rest}${U}` ? true : false
type E0 = EndsWith<'123', '23'> // true
type E11 = EndsWith<'123', '13'> // false
type E2 = EndsWith<'123', '123'> // true

@dolphin0618
Copy link

type StartsWith<T extends string, U extends string> = T extends `${U}${string}` ? true : false

type S0 = StartsWith<'123', '12'> // true
type S1 = StartsWith<'123', '13'> // false
type S2 = StartsWith<'123', '1234'> // false


type EndsWith<T extends string, U extends string> = T extends `${string}${U}` ? true : false

type E0 = EndsWith<'123', '23'> // true
type E1 = EndsWith<'123', '13'> // false
type E2 = EndsWith<'123', '123'> // true

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

6 participants