You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(store): enforce exact return types in on() callbacks
The on() function now enforces exact return types at the type level,
preventing excess properties in callback return values without requiring
explicit return type annotations.
This eliminates the need for the on-function-explicit-return-type ESLint
rule, which is now deprecated.
BREAKING CHANGES:
Reducer callbacks passed to on() that return objects with properties not
present in the state type will now produce TypeScript compilation errors.
BEFORE:
```ts
interface State { name: string; count: number }
const initialState: State = { name: '', count: 0 };
const reducer = createReducer(
initialState,
on(setName, (state, { name }) => ({
...state,
name,
extraProp: true, // no error
})),
);
```
AFTER:
```ts
// Now produces a TypeScript error:
// Type '{ extraProp: boolean; ... }' is not assignable to type
// '"callback return type must exactly match the state type.
// Remove excess properties."'
const reducer = createReducer(
initialState,
on(setName, (state, { name }) => ({
...state,
name,
extraProp: true, // TS error
})),
);
```
Closes#4280
Copy file name to clipboardExpand all lines: projects/www/src/app/pages/guide/eslint-plugin/rules/on-function-explicit-return-type.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,7 @@
3
3
`On` function should have an explicit return type.
4
4
5
5
-**Type**: suggestion
6
+
-**Deprecated**: Yes
6
7
-**Fixable**: No
7
8
-**Suggestion**: Yes
8
9
-**Requires type checking**: No
@@ -11,6 +12,10 @@
11
12
<!-- Everything above this generated, do not edit -->
12
13
<!-- MANUAL-DOC:START -->
13
14
15
+
## Deprecation Notice
16
+
17
+
This rule is deprecated. The `on` function now enforces exact return types at the type level, so excess properties in `on` callbacks will produce TypeScript compilation errors without needing an explicit return type annotation.
18
+
14
19
## Rule Details
15
20
16
21
When we use the `on` function to create reducers, we usually copy the state into a new object, and then add the properties that are being modified after that certain action. This may result in unexpected typing problems, we can add new properties into the state that did not exist previously. TypeScript doesn't see this as a problem and might change the state's interface. The solution is to provide an explicit return type to the `on` function callback.
0 commit comments