✍ 공부/TypeScript

[type-challenges] Drop Char

Po_tta_tt0 2023. 7. 25. 19:45
반응형

문제

문자열에서 지정된 문자를 제거합니다

설명

/* _____________ Your Code Here _____________ */

type DropChar<S, C, T extends string = ""> = S extends `${infer A}${infer B}`
? A extends C 
? DropChar<B,C,T> 
: DropChar<B,C,`${T}${A}`>
:T


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

type cases = [
  // @ts-expect-error
  Expect<Equal<DropChar<'butter fly!', ''>, 'butterfly!'>>,
  Expect<Equal<DropChar<'butter fly!', ' '>, 'butterfly!'>>,
  Expect<Equal<DropChar<'butter fly!', '!'>, 'butter fly'>>,
  Expect<Equal<DropChar<'    butter fly!        ', ' '>, 'butterfly!'>>,
  Expect<Equal<DropChar<' b u t t e r f l y ! ', ' '>, 'butterfly!'>>,
  Expect<Equal<DropChar<' b u t t e r f l y ! ', 'b'>, '  u t t e r f l y ! '>>,
  Expect<Equal<DropChar<' b u t t e r f l y ! ', 't'>, ' b u   e r f l y ! '>>,
]

 

타입스크립트로 문자열을 다루는 방법을 알면 해결할수 있다!

따붕extends야 고마워👍

반응형