zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Typescript] 48. Medium - EndsWith

typescript Medium 48
2023-09-14 08:59:11 时间

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>>,
]