'Why I get: Property 'length' does not exist on type 'never'
There is an array of objects in the redux store. When I would get it through useSelector there was no problem.
Then I extracted the code to a util function, and I used store.getState() so then I got the above error.
I've found this post so then I imported the array through args and I've putted in the types of the objects in the array a length property.
Now it works, but I don't like it like this.
Is there a way to get it from store.getState()?
Am I doing something wrong?
Minimal reproducible example, here in codesandbox
export interface MultiQuestionProps {
...
length: number;
}
/* Pass questions as arg, to avoid TS error with length. */
const getDownloadedQuestionsNum = (
// questionsMultiMixed: MultiQuestionProps,
) => {
const { categoryId, gameTimer, gameType } = store.getState().general;
...
const {
questionsMultiMixed,
} = store.getState().questions;
let downloadedQuestionsNum = 0;
if (gameType === constStr.multi) {
switch (categoryId) {
case constStr.mixed:
if (gameTimer)
downloadedQuestionsNum = questionsMultiMixed.length - indexMultiMixedWithTimer;
else downloadedQuestionsNum = questionsMultiMixed.length - indexMultiMixedNoTimer;
...
setDownloadedQuestionsNum(getDownloadedQuestionsNum(
questionsMultiMixed,
))
reducer
interface InitialStateProps {
questionsMultiMixed: MultiQuestionProps[];
}
const initialState: InitialStateProps = {
questionsMultiMixed: [],
...
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
