-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00268-easy-if.ts
40 lines (29 loc) · 1.02 KB
/
00268-easy-if.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
/*
268 - If
-------
by Pavel Glushkov (@pashutk) #easy #utils
### Question
Implement a utils `If` which accepts condition `C`, a truthy return type `T`, and a falsy return type `F`. `C` is expected to be either `true` or `false` while `T` and `F` can be any type.
For example:
```ts
type A = If<true, 'a', 'b'> // expected to be 'a'
type B = If<false, 'a', 'b'> // expected to be 'b'
```
> View on GitHub: https://tsch.js.org/268
*/
/* _____________ Your Code Here _____________ */
type If<C extends Boolean, T, F> = C extends true ? T : F;
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from "@type-challenges/utils";
type cases = [
Expect<Equal<If<true, "a", "b">, "a">>,
Expect<Equal<If<false, "a", 2>, 2>>
];
// @ts-expect-error
type error = If<null, "a", "b">;
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/268/answer
> View solutions: https://tsch.js.org/268/solutions
> More Challenges: https://tsch.js.org
*/