Skip to content

Commit fe3ec16

Browse files
committed
Added 29.2
1 parent ba11a24 commit fe3ec16

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { it } from "vitest";
2+
3+
interface Events {
4+
click: {
5+
x: number;
6+
y: number;
7+
};
8+
focus: undefined;
9+
}
10+
11+
export const sendEvent = (event: keyof Events, ...args: any[]) => {
12+
// Send the event somewhere!
13+
};
14+
15+
it("Should force you to pass a second argument when you choose an event with a payload", () => {
16+
// @ts-expect-error
17+
sendEvent("click");
18+
19+
sendEvent("click", {
20+
// @ts-expect-error
21+
x: "oh dear",
22+
});
23+
24+
sendEvent(
25+
"click",
26+
// @ts-expect-error
27+
{
28+
y: 1,
29+
}
30+
);
31+
32+
sendEvent("click", {
33+
x: 1,
34+
y: 2,
35+
});
36+
});
37+
38+
it("Should prevent you from passing a second argument when you choose an event without a payload", () => {
39+
sendEvent("focus");
40+
41+
sendEvent(
42+
"focus",
43+
// @ts-expect-error
44+
{}
45+
);
46+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { it } from "vitest";
2+
3+
interface Events {
4+
click: {
5+
x: number;
6+
y: number;
7+
};
8+
focus: undefined;
9+
}
10+
11+
export const sendEvent = <TEventKey extends keyof Events>(
12+
event: TEventKey,
13+
...args: Events[TEventKey] extends undefined
14+
? []
15+
: [payload: Events[TEventKey]]
16+
) => {
17+
// Send the event somewhere!
18+
};
19+
20+
it("Should force you to pass a second argument when you choose an event with a payload", () => {
21+
// @ts-expect-error
22+
sendEvent("click");
23+
24+
sendEvent("click", {
25+
// @ts-expect-error
26+
x: "oh dear",
27+
});
28+
29+
sendEvent(
30+
"click",
31+
// @ts-expect-error
32+
{
33+
y: 1,
34+
},
35+
);
36+
37+
sendEvent("click", {
38+
x: 1,
39+
y: 2,
40+
});
41+
});
42+
43+
it("Should prevent you from passing a second argument when you choose an event without a payload", () => {
44+
sendEvent("focus");
45+
46+
sendEvent(
47+
"focus",
48+
// @ts-expect-error
49+
{},
50+
);
51+
});

0 commit comments

Comments
 (0)