✍ 공부/TypeScript

[type-challenges] EndsWith

Po_tta_tt0 2023. 8. 1. 13:24
반응형

문제

두개의 string 타입을 받아 TU로 끝나는지 여부를 반환하는 EndsWith<T,U>를 구현하세요.

설명

/* _____________ Your Code Here _____________ */

type EndsWith<T extends string, U extends string> =
U extends ""
? true
:T extends U
? true
:T extends `${infer A}${infer B}`
? B extends U
? true
: false
: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>>,
  Expect<Equal<EndsWith<'abc', 'ac'>, false>>,
  Expect<Equal<EndsWith<'abc', ''>, true>>,
  Expect<Equal<EndsWith<'abc', ' '>, false>>,
]

 

 

반응형