'What should the return type be for Redux selectors?
In my definition of a Redux store I have
import { Device } from "react-native-ble-plx"
import { configureStore, createSlice } from '@reduxjs/toolkit'
const bleSlice = createSlice( {
name: 'ble',
initialState: {scannedDevices: [], isScanning: false} as StoreBLEState,
reducers: {
deviceAdd(state, action) {
state.scannedDevices.push(action.payload)
},
scanIsRunning(state, action) {
state.isScanning = action.payload
},
devicesClear(state) {
state.scannedDevices = []
},
}
} )
export const storeBLE = { ...bleSlice.actions }
export const store = configureStore( {
reducer: {
ble: bleSlice.reducer,
}
} )
export interface StoreBLEDeviceInfo {
name: string | null;
id: string;
}
export interface StoreBLEState {
scannedDevices: StoreBLEDeviceInfo[];
isScanning: boolean;
}
export type StoreRootState = ReturnType<typeof store.getState>
which I use in a component with something like
const bleState: StoreBLEState = useSelector((state: StoreRootState) => state.ble)
and elsewhere with, for example
store.dispatch(storeBLE.devicesClear())
These last two uses, as well as each of the function definitions under reducers get flagged by my type checking (the latter with "Void function return value is used" and the first two for missing a return type specification).
- What is the return type of
(state: StoreRootState) => state.ble? - What is the return type of each of the functions specified under
reducersin the first code fragment (e.g of{deviceAdd(state, action) { ... })?
Solution 1:[1]
This is not a flagging by a type checker, but probably by a linter rule. Honestly, I'd disable that linter rule - it doesn't add any value.
For the question though:
deviceAdd(state, action): void {
state.scannedDevices.push(action.payload)
},
and
(state: StoreRootState): StoreBLEState => state.ble
(you would have gotten that one just by hovering over it)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | phry |
