awesome-typescript icon indicating copy to clipboard operation
awesome-typescript copied to clipboard

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

Open semlinker opened this issue 2 years ago • 6 comments

实现 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

请在下面评论你的答案

semlinker avatar Sep 25 '21 13:09 semlinker

// 实现 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

zhaoxiongfei avatar Sep 26 '21 00:09 zhaoxiongfei

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等类型

mingzhans avatar Oct 07 '21 07:10 mingzhans

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;

zjxxxxxxxxx avatar Mar 24 '22 06:03 zjxxxxxxxxx

// 实现 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

ChuTingzj avatar Jun 14 '22 12:06 ChuTingzj

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

dolphin0618 avatar Jul 24 '22 02:07 dolphin0618