[Typescript] 48. Medium - EndsWith

Implement EndsWith<T, U> which takes two exact string types and returns whether T ends with U

For example:

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

 

/* _____________ Your Code Here _____________ */

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


/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<EndsWith<'abc', 'bc'>, true>>,
  Expect<Equal<EndsWith<'abc', 'abc'>, true>>,
  Expect<Equal<EndsWith<'abc', 'd'>, false>>,
]

 

posted @ 2022-10-14 01:40  Zhentiw  阅读(16)  评论(0编辑  收藏  举报