Skip to content

EndsWith

题目

实现EndsWith<T, U>,接收两个 string 类型参数,然后判断T是否以U结尾,根据结果返回truefalse

例如:

typescript
type a = EndsWith<"abc", "bc">; // expected to be true
type b = EndsWith<"abc", "abc">; // expected to be true
type c = EndsWith<"abc", "d">; // expected to be false

解答

StartsWith,反向推导即可:

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

Released under the MIT License.