-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07561-extreme-subtract.ts
60 lines (43 loc) · 1.34 KB
/
07561-extreme-subtract.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
49
50
51
52
53
54
55
56
57
58
59
/*
7561 - Subtract
-------
by Lo (@LoTwT) #extreme #tuple
### Question
Implement the type Subtraction that is ` - ` in Javascript by using BuildTuple.
If the minuend is less than the subtrahend, it should be `never`.
It's a simple version.
For example
```ts
Subtract<2, 1> // expect to be 1
Subtract<1, 2> // expect to be never
```
> View on GitHub: https://tsch.js.org/7561
*/
/* _____________ Your Code Here _____________ */
type ToArray<Number extends number, _Result extends any[] = []> =
_Result extends { length: Number }
? _Result
: ToArray<Number, [..._Result, any]>
type Subtract<M extends number, S extends number> =
ToArray<M> extends ToArray<S>
? 0
: ToArray<M> extends [...infer Diff, ToArray<S>]
? Diff['length'] extends 0
? never
: Diff['length']
: never
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Subtract<1, 1>, 0>>,
Expect<Equal<Subtract<2, 1>, 1>>,
Expect<Equal<Subtract<1, 2>, never>>,
// @ts-expect-error
Expect<Equal<Subtract<1000, 999>, 1>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/7561/answer
> View solutions: https://tsch.js.org/7561/solutions
> More Challenges: https://tsch.js.org
*/