Language/Typescript
타입챌린지 : 2688-StartsWith (medium)
hsloth
2023. 5. 8. 13:50
이 글은 제가 타입챌린지를 하면서 해석한 내용을 적는 글입니다. 틀린 내용이 있으면 댓글 달아주시면 감사하겠습니다.
GitHub - type-challenges/type-challenges: Collection of TypeScript type challenges with online judge
Collection of TypeScript type challenges with online judge - GitHub - type-challenges/type-challenges: Collection of TypeScript type challenges with online judge
github.com
T가 U로 시작하는지 true / false를 리턴하는 타입이다.
어... 엄청 간단하다. 설명은 안하겠다...!
type StartsWith<T extends string, U extends string> = T extends `${U}${infer O}` ?
true : false
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<StartsWith<'abc', 'ac'>, false>>,
Expect<Equal<StartsWith<'abc', 'ab'>, true>>,
Expect<Equal<StartsWith<'abc', 'abc'>, true>>,
Expect<Equal<StartsWith<'abc', 'abcd'>, false>>,
Expect<Equal<StartsWith<'abc', ''>, true>>,
Expect<Equal<StartsWith<'abc', ' '>, false>>,
Expect<Equal<StartsWith<'', ''>, true>>,
]