Skip to content

Commit a17058e

Browse files
committed
Fixed useReducer
1 parent daab585 commit a17058e

4 files changed

+57
-38
lines changed

src/03-hooks/21-use-reducer.problem.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { Equal, Expect } from "../helpers/type-utils";
44
const reducer = (state: unknown, action: unknown) => {
55
switch (action.type) {
66
case "add":
7-
return { count: state.count + action.amount };
7+
return { count: state.count + action.add };
88
case "subtract":
9-
return { count: state.count - action.amount };
9+
return { count: state.count - action.subtract };
1010
default:
1111
throw new Error();
1212
}
@@ -16,13 +16,13 @@ const [state, dispatch] = useReducer(reducer, { count: 0 });
1616

1717
type tests = [Expect<Equal<typeof state.count, number>>];
1818

19-
dispatch({ type: "add", amount: 1 });
19+
dispatch({ type: "add", add: 1 });
2020

2121
// @ts-expect-error
22-
dispatch({ type: "SUBTRACT" });
22+
dispatch({ type: "SUBTRACT", subtract: 1 });
2323

2424
// @ts-expect-error
2525
dispatch({ type: "add" });
2626

2727
// @ts-expect-error
28-
dispatch({ type: "subtract", amount: "123" });
28+
dispatch({ type: "subtract", subtract: "123" });

src/03-hooks/21-use-reducer.solution.1.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ const reducer = (
55
state: {
66
count: number;
77
},
8-
action: {
9-
type: string;
10-
amount: number;
11-
}
8+
action:
9+
| {
10+
type: "add";
11+
add: number;
12+
}
13+
| {
14+
type: "subtract";
15+
subtract: number;
16+
}
1217
) => {
1318
switch (action.type) {
1419
case "add":
15-
return { count: state.count + action.amount };
20+
return { count: state.count + action.add };
1621
case "subtract":
17-
return { count: state.count - action.amount };
22+
return { count: state.count - action.subtract };
1823
default:
1924
throw new Error();
2025
}
@@ -24,13 +29,13 @@ const [state, dispatch] = useReducer(reducer, { count: 0 });
2429

2530
type tests = [Expect<Equal<typeof state.count, number>>];
2631

27-
dispatch({ type: "add", amount: 1 });
32+
dispatch({ type: "add", add: 1 });
2833

2934
// @ts-expect-error
30-
dispatch({ type: "SUBTRACT" });
35+
dispatch({ type: "SUBTRACT", subtract: 1 });
3136

3237
// @ts-expect-error
3338
dispatch({ type: "add" });
3439

3540
// @ts-expect-error
36-
dispatch({ type: "subtract", amount: "123" });
41+
dispatch({ type: "subtract", subtract: "123" });
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
import { useReducer } from "react";
22
import { Equal, Expect } from "../helpers/type-utils";
33

4-
type ReducerAction =
5-
| { type: "add"; amount: number }
6-
| { type: "subtract"; amount: number };
7-
8-
type ReducerState = { count: number };
4+
type ReducerState = {
5+
count: number;
6+
};
97

10-
const reducer = (state: ReducerState, action: ReducerAction): ReducerState => {
8+
type ReducerAction =
9+
| {
10+
type: "add";
11+
add: number;
12+
}
13+
| {
14+
type: "subtract";
15+
subtract: number;
16+
};
17+
18+
const reducer = (state: ReducerState, action: ReducerAction) => {
1119
switch (action.type) {
1220
case "add":
13-
return { count: state.count + action.amount };
21+
return { count: state.count + action.add };
1422
case "subtract":
15-
return { count: state.count - action.amount };
23+
return { count: state.count - action.subtract };
1624
default:
1725
throw new Error();
1826
}
@@ -22,13 +30,13 @@ const [state, dispatch] = useReducer(reducer, { count: 0 });
2230

2331
type tests = [Expect<Equal<typeof state.count, number>>];
2432

25-
dispatch({ type: "add", amount: 1 });
33+
dispatch({ type: "add", add: 1 });
2634

2735
// @ts-expect-error
28-
dispatch({ type: "SUBTRACT" });
36+
dispatch({ type: "SUBTRACT", subtract: 1 });
2937

3038
// @ts-expect-error
3139
dispatch({ type: "add" });
3240

3341
// @ts-expect-error
34-
dispatch({ type: "subtract", amount: "123" });
42+
dispatch({ type: "subtract", subtract: "123" });
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import { Reducer, useReducer } from "react";
22
import { Equal, Expect } from "../helpers/type-utils";
33

4-
type ReducerAction =
5-
| { type: "add"; amount: number }
6-
| { type: "subtract"; amount: number };
7-
8-
type ReducerState = { count: number };
9-
10-
type MyReducer = Reducer<ReducerState, ReducerAction>;
4+
type ReducerState = {
5+
count: number;
6+
};
117

12-
const reducer: MyReducer = (state, action) => {
8+
type ReducerAction =
9+
| {
10+
type: "add";
11+
add: number;
12+
}
13+
| {
14+
type: "subtract";
15+
subtract: number;
16+
};
17+
18+
const reducer: Reducer<ReducerState, ReducerAction> = (state, action) => {
1319
switch (action.type) {
1420
case "add":
15-
return { count: state.count + action.amount };
21+
return { count: state.count + action.add };
1622
case "subtract":
17-
return { count: state.count - action.amount };
23+
return { count: state.count - action.subtract };
1824
default:
1925
throw new Error();
2026
}
@@ -24,13 +30,13 @@ const [state, dispatch] = useReducer(reducer, { count: 0 });
2430

2531
type tests = [Expect<Equal<typeof state.count, number>>];
2632

27-
dispatch({ type: "add", amount: 1 });
33+
dispatch({ type: "add", add: 1 });
2834

2935
// @ts-expect-error
30-
dispatch({ type: "SUBTRACT" });
36+
dispatch({ type: "SUBTRACT", subtract: 1 });
3137

3238
// @ts-expect-error
3339
dispatch({ type: "add" });
3440

3541
// @ts-expect-error
36-
dispatch({ type: "subtract", amount: "123" });
42+
dispatch({ type: "subtract", subtract: "123" });

0 commit comments

Comments
 (0)