Skip to content

Commit 854942a

Browse files
authored
Add TypeScript guideline for minimizing selector input types (#140)
- Closes #139
1 parent 3c07983 commit 854942a

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

docs/typescript.md

+88
Original file line numberDiff line numberDiff line change
@@ -1064,3 +1064,91 @@ async function removeAccount(address: Hex): Promise<KeyringControllerState> {
10641064
return this.fullUpdate();
10651065
}
10661066
```
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.
1071+
1072+
**Example <a id="example-ef791329-ec32-477f-b17f-efeed019a42e"></a> ([🔗 permalink](#example-ef791329-ec32-477f-b17f-efeed019a42e)):**
1073+
1074+
🚫
1075+
1076+
```typescript
1077+
const selectTodos = (state: RootState) => {
1078+
const {
1079+
todoSlice: { todosA, todosB },
1080+
} = state;
1081+
return { todosA, todosB };
1082+
};
1083+
```
1084+
1085+
1086+
1087+
```typescript
1088+
const selectTodos = (state: {
1089+
todoSlice: Pick<RootState['todoSlice'], 'todosA' | 'todosB'>;
1090+
}) => {
1091+
const {
1092+
todoSlice: { todosA, todosB },
1093+
} = state;
1094+
return { todosA, todosB };
1095+
};
1096+
```
1097+
1098+
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.
1102+
1103+
**Example <a id="example-85437d62-3d51-489a-80c9-cd7639ad6937"></a> ([🔗 permalink](#example-85437d62-3d51-489a-80c9-cd7639ad6937)):**
1104+
1105+
🚫
1106+
1107+
```typescript
1108+
const selectPropA = (state: RootState) => state.sliceA.propA;
1109+
const selectPropB = (state: RootState) => state.sliceB.propB;
1110+
1111+
// As its first argument, `selectResult` expects a state object of type `RootState`
1112+
const selectResult =
1113+
createSelector([selectPropA, selectPropB], (propA, propB) => ...);
1114+
```
1115+
1116+
1117+
1118+
```typescript
1119+
const selectPropA = (state: { sliceA: { propA: string } })
1120+
=> state.sliceA.propA;
1121+
const selectPropB = (state: { sliceB: { propB: number } })
1122+
=> state.sliceA.propB;
1123+
1124+
// As its first argument, `selectResult` expects a state object of the following type:
1125+
// `{ sliceA: { propA: string } } & { sliceB: { propB: number } }`
1126+
const selectResult =
1127+
createSelector([selectPropA, selectPropB], (propA, propB) => ...);
1128+
```
1129+
1130+
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

Comments
 (0)