• 欢迎光临~

[Typescript] 48. Medium - EndsWith

开发技术 开发技术 2022-10-14 次浏览

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

 

程序员灯塔
转载请注明原文链接:[Typescript] 48. Medium - EndsWith
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com