-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00529-medium-absolute.ts
52 lines (38 loc) · 1.27 KB
/
00529-medium-absolute.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
/*
529 - Absolute
-------
by Andrey Krasovsky (@bre30kra69cs) #medium #math #template-literal
### Question
Implement the `Absolute` type. A type that take string, number or bigint. The output should be a positive number string
For example
```ts
type Test = -100;
type Result = Absolute<Test>; // expected to be "100"
```
> View on GitHub: https://tsch.js.org/529
*/
/* _____________ Your Code Here _____________ */
type Absolute<T extends number | string | bigint> =
`${T}` extends `-${infer Modulus}`
? Modulus
: `${T}`
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Absolute<0>, '0'>>,
Expect<Equal<Absolute<-0>, '0'>>,
Expect<Equal<Absolute<10>, '10'>>,
Expect<Equal<Absolute<-5>, '5'>>,
Expect<Equal<Absolute<'0'>, '0'>>,
Expect<Equal<Absolute<'-0'>, '0'>>,
Expect<Equal<Absolute<'10'>, '10'>>,
Expect<Equal<Absolute<'-5'>, '5'>>,
Expect<Equal<Absolute<-1_000_000n>, '1000000'>>,
Expect<Equal<Absolute<9_999n>, '9999'>>,
]
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/529/answer
> View solutions: https://tsch.js.org/529/solutions
> More Challenges: https://tsch.js.org
*/