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
Copy file name to clipboardExpand all lines: docs/typescript.md
+88
Original file line number
Diff line number
Diff line change
@@ -1064,3 +1064,91 @@ async function removeAccount(address: Hex): Promise<KeyringControllerState> {
1064
1064
returnthis.fullUpdate();
1065
1065
}
1066
1066
```
1067
+
1068
+
#### For selector functions, define the input state argument with the narrowest type that preserves functionality
1069
+
1070
+
A selector function that directly queries state properties should define its input state argument as a subtype of root state that only contains the required queried properties.
A selector function that is derived via composition of input selectors should ensure that the input state argument of the output function is defined by merging the input selectors' state argument types.
1099
+
1100
+
> [!TIP]
1101
+
> This is the default behavior of the `reselect` library, so there is no need to explicitly define a merged type for the result function when using the `createSelector` or `createDeepEqualSelector` methods.
Selectors must be both composable and atomic. If all input selectors are typed homogeneously, these can become conflicting objectives.
1131
+
1132
+
- **Composable:**
1133
+
1134
+
Without heterogeneous typing for selectors, the size of the state type for all selectors tends to inflate. This is because a selector's state must be a supertype of the intersection of all input selector state types, including selectors that are nested in the definitions of input selectors.
1135
+
1136
+
Eventually, many selectors end up being defined with a state type that is close to or equal to the entire Redux state.
1137
+
1138
+
Paradoxically, selectors that only need access to very few properties end up needing to have the widest state type, because they tend to be merged into the most selectors across several nested levels.
1139
+
1140
+
- **Atomic:**
1141
+
1142
+
When selectors are actually invoked, including in test files, it's not always practical to prepare and pass in a very large state object.
1143
+
1144
+
It's both safer and more convenient to restrict the state argument type of the selector to the minimum size required for the selector to function. Note that this does not prevent the selector from accepting a larger state object, but it does allow the selector to function with an incomplete state object, as long as it satisfies the input argument type.
1145
+
1146
+
This requirement becomes incompatible with the composability requirement if all selectors must share a homogeneous state type.
1147
+
Enabling composed selectors to accept different, even disjoint state types resolves this issue.
1148
+
1149
+
> [!NOTE]
1150
+
> At runtime, all selectors are passed the entire Redux state, which is always assignable to the narrower state argument type.
1151
+
>
1152
+
> Following this guideline does not affect selector memoization. When background state updates are dispatched to the Redux store, the Redux state object is shallow copied. This does not mutate the references of nested composite data structures, which makes cache invalidation a non-concern.
1153
+
>
1154
+
> The only exception to this is an idempotent selector that returns the entire Redux state (e.g. `(state:RootState) =>state`). This pattern should be avoided if possible, as it will cause a performance hit to any downstream selector.
0 commit comments