-
Notifications
You must be signed in to change notification settings - Fork 0
/
2693-endswith.ts
48 lines (37 loc) · 1.37 KB
/
2693-endswith.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
2693 - EndsWith
-------
by jiangshan (@jiangshanmeta) #medium #template-literal
### Question
Implement `EndsWith<T, U>` which takes two exact string types and returns whether `T` ends with `U`
For example:
```typescript
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
```
> View on GitHub: https://tsch.js.org/2693
*/
/* _____________ Your Code Here _____________ */
type EndsWith<T extends string, U extends string> = T extends `${infer F}${U}` ? true : false;
type EndsWithMy<T extends string, U extends string> = U extends T
? true
: T extends `${infer T1}${infer Rest}`
? EndsWith<Rest, U>
: 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>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/2693/answer
> View solutions: https://tsch.js.org/2693/solutions
> More Challenges: https://tsch.js.org
*/