The operation state.concat cannot be performed as it is meant for arrays and not for type 'ITask'

Trying to incorporate an array into my store is causing issues with the following error popping up at

tasks: payload.payload ? [ ...state,  payload.payload ] : []
:
Error:(22, 35) TS2461: Type 'ITask' is not an array type
. Any suggestions on how to resolve this?

import * as TaskActions from './task.actions';
import { Action, createReducer, on } from '@ngrx/store';
import { ITask } from '../../models/task';

export interface State {
  task: ITask | null;
  error: any;
}

const initialState: ITask = {
  basketTotal: 0,
  carePlanPrice: 0,
  category: null,
  completionDate: null
};

export const taskReducer = createReducer(
  initialState,
  on(TaskActions.getData, state => ({ ...state })),
  on(TaskActions.dataReceived, (state, payload) => ({
    ...state,
    tasks: payload.payload ? [ ...state,  payload.payload ] : []
  })),
  on(TaskActions.dataNotReceived, state => ({ ...state })),
  on(TaskActions.signOut, state => ({ ...state })),
  on(TaskActions.signOutSuccess, state => ({ ...state, ...initialState })),
);

export function reducer(state: ITask | undefined, action: Action) {
  return taskReducer(state, action);
}

Snapshot:

https://i.sstatic.net/93nVJ.png

Answer №1

Assignment, data are not arrays. Therefore, the usage of the spread operator is unnecessary.

assignments: data.data ? [ record,  data.data ] : []

According to MDN:

The Spread syntax allows for an iterable like an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.

If you wish to add them as objects:

assignments: data.data ? [ { ...record}, {...data.data} ] : []

Answer №2

To update your code, make the following changes:

const initialState: State = {
  // Add default initial state here
};

Make sure to use the State interface for initialState, not ITask.

Since your interface only has 2 properties:

task: ITask | null;
error: any;

Update your initialState to:

const initialState: State = {
  task: null,
  error: null
};

Next, update your reducer:

on(TaskActions.dataReceived, (state, payload) => ({
    ...state,
    task: payload.payload ? [ ...state,  payload.payload ] : []
  })),

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS directive doesn't refresh template when scope values are fetched dynamically through ajax requests

Attempting to give this question a precise title as possible, I find myself struggling with an issue in AngularJS. While trying to showcase my problem through a jsfiddle, it turned out to be too reliant on multiple files and not yet accessible online. So p ...

Disabling dates in Kendo Date Time Picker (Angular): An easy guide

<input id="startDate" kendo-date-time-picker k-ng-model="vm.startDate" k-on-change="vm.updateStartDate()" required /> Can someone please explain how to incorporate disabled dates into this date picker without utilizi ...

Error with Jquery authentication

Take a look at the code snippet below: var jsonData1 = $.ajax({ url: 'http://'+domainName+':9898/jolokia/read/*', dataType:"json", crossDomain: true, beforeSend: function(xhr){ var username= '*&a ...

Creating reversed/generate URLs from Routes in Angular2 (using typescript)

Is there a way to automatically create a URL from a route using code? For instance, let's say I have a login component defined in my routes: const appRoutes: Routes = [ ... { path: 'login', component: LoginComponent }, ... ]; N ...

producing imperfections on tiny items

I am currently working on rendering a texture onto a cylinder using threeJS. While it usually works well, I have encountered some strange artifacts when the radius of the cylinder is set to very low values (such as 0.0012561892224928503 in the image attac ...

Change the spread operator in JavaScript to TypeScript functions

I'm struggling to convert a piece of code from Javascript to Typescript. The main issue lies in converting the spread operator. function calculateCombinations(first, next, ...rest) { if (rest.length) { next = calculateCombinations(next, ...res ...

Ensuring the cookie remains active throughout various subdomains

After implementing code to set a cookie on example.com, I noticed an issue with it carrying over to the subdomain dogs.example.com. <script> document.cookie="cid1={{utm_campaign}}; path=/;" </script> The {{}} is part of Google-Tag-Manager s ...

Bootstrap 4 Multilevel Navbar with Bootswatch design positioned on the right side

How can I modify a multilevel Navbar in Bootswatch 4 (Bootstrap 4) to make the Submenus pop open to the left side? The "dropdown-menu-right" class doesn't achieve the desired result. Can someone provide guidance on how the CSS / Javascript should be a ...

Utilizing the jQuery slideToggle method on the specified target element

I'm encountering an issue with jQuery slideToggle, and here's the code I have: $(".dropdownmainmenu").click(function(){ $(this).children(".showmenu").slideToggle(); $(this).toggleClass("activemainmenu"); $(this).find(".showarrowmainmen ...

Angular - Managing unexpected routes for customized functionality

I am in the process of creating an app with Angular 6 and I'm faced with handling unknown routes within my application. For instance: Instagram has a default page (instagram.com), but if you type in (instagram.com/user) it redirects you to the user ...

Explore the versatile Bootstrap Table for class

Recently, I created a table with the following structure: <table id="table" class="table table-bordered table-hover"> <thead> <tr> <th data-field="id" class="hidden">ID</th> <th data-fie ...

We were unable to locate the requested resource

I have been working on setting up an Express endpoint to fetch comments or reviews of a movie based on the movie ID. In my initial route, I manually passed the ID and retrieved data from TheMovieDB. However, I wanted to make this process dynamic in my seco ...

Angular issue: Unable to implement Bootstrap dropdown in navbar

I've successfully added popper.js, bootstrap, and jquery to my project. Here is my Angular json file configuration: "styles": [ "src/styles.css", "../node_modules/bootstrap/dist/css/bootstrap.min.css" ], "script ...

The declaration file for 'autobind-decorator' is missing in TypeScript and cannot be located

Having a bit of trouble with my Typescript project. I'm trying to import the 'autobind-decorator' package, but I hit a roadblock. When compiling, I keep running into this error: cannot find declaration file for 'autobind-decorator&ap ...

Navigate to the correct page when the Button is clicked in a ReactJS/Django project

Embarking on my web development journey, I opted for django/Reactjs to build a social network platform. I created several API methods like account/view_user/ to retrieve a list of users and account/view_user/ for specific user attributes. In React, I cra ...

Can a form component be recycled through the use of inheritance?

Greetings to the Stackoverflow Community, As I delve into this question, I realize that examples on this topic are scarce. Before diving into specifics, let me outline my strategy. I currently have three pages with numerous FormGroups that overlap signif ...

Multiple Components Sharing the Same ID Attribute in Angular

Recently, I discovered that using the same id attribute for HTML elements in multiple components can lead to repetition of the ID in the DOM when those components are rendered together in the view. Let's consider the following scenario: //hello.comp ...

What are some strategies for customizing the appearance of child components within a parent component?

I have a scenario where I am using parent and child components. When I use the HTML in another component, I also apply my CSS. For example, in my parent component: HTML <div class="chips"> <p class="tags">Tag 1</p&g ...

Encountering issues with loading series on Highchart in Angular 4 due to duplication restrictions

I recently integrated highchart into my Angular 4 application, but I encountered a problem where the same series is being loaded twice. The issue seems to be related to the addSeries method, which is triggered by the @Input() set stressTestResults declarat ...

Managing nested request bodies in NestJS for POST operations

A client submits the following data to a REST endpoint: { "name":"Harry potter", "address":{ "street":"ABC Street", "pincode":"123", "geo":{ &q ...