Skip to content

Commit e03c527

Browse files
committed
add day 18
1 parent 6f8b691 commit e03c527

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

2024/18/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { Expect, Equal } from "type-testing";
2+
3+
const createStreetLight = <T>(colors: T[], defaultColor: NoInfer<T>) => {
4+
console.log(colors);
5+
return defaultColor;
6+
};
7+
8+
// ------------------- Test section ---------------------
9+
10+
const colors = ["red" as const, "yellow" as const, "green" as const];
11+
// ^?
12+
type Color = (typeof colors)[number];
13+
// ^?
14+
15+
// red is a valid color, no generic parameters needed
16+
const t0_const = createStreetLight(colors, "red");
17+
type t0_actual = typeof t0_const; // =>
18+
type t0_expected = Color; // =>
19+
type t0 = Expect<Equal<t0_actual, t0_expected>>;
20+
21+
// one generic parameter is ok
22+
const t1_const = createStreetLight<Color>(colors, "red");
23+
type t1_actual = typeof t1_const; // =>
24+
type t1_expected = Color; // =>
25+
type t1 = Expect<Equal<t1_actual, t1_expected>>;
26+
27+
// @ts-expect-error (no generic parameters) blue is not a valid option
28+
const e0 = createStreetLight(colors, "blue");
29+
30+
// @ts-expect-error does not accept two generic parameters, even providing a valid option
31+
const e1 = createStreetLight<Color, "red">(colors, "red");
32+
33+
// @ts-expect-error does not accept two generic parameters, and blue isn't a valid option
34+
const e2 = createStreetLight<Color, "blue">(colors, "blue");
35+
36+
// @ts-expect-error (with one generic parameter) blue is not a valid option
37+
const e3 = createStreetLight<Color>(colors, "blue");

0 commit comments

Comments
 (0)