A guide on implementing Redux Toolkit in an SPFX (typescript) project

I am currently working on developing SPFX components using typescript and webpack.

My goal is to integrate redux toolkit into my project, but it is causing compilation errors.

Here is what I have done so far:

  • Created the project structure using yo
  • Installed redux, react-redux, and @reduxjs/toolkit (using pnpm)
  • Followed the Redux Toolkit TypeScript Quick Start by creating the file store.ts with the provided content.
import { configureStore } from '@reduxjs/toolkit'
// ...

const store = configureStore({
  reducer: {
  
  }
})

// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch

When attempting to build the project using gulp build, it fails with multiple errors:

 (list of error messages removed for brevity)

Additionally, I am receiving type inference errors in VS Code:

The inferred type of 'store' cannot be named without a reference to '.pnpm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d1f080918154019051803062d5f435e435d">[email protected]</a>/node_modules/redux-thunk'. This is likely not portable. A type annotation is necessary.

To address this issue, I found that installing redux-thunk and adding import 'redux-thunk' at the beginning of the file resolved the error.

Could someone provide guidance on the correct setup for my project?

For reference, here is a repository containing the code that reproduces the error

[Edit] Switching back to using classic npm resulted in similar issues (with the exception of displaying local file paths instead of linked ones when using pnpm).

Answer №1

I have uncovered the answer.

The underlying issue stems from the syntax used in react redux, which now demands typescript 3.8 or later.

Previously, the usage of import type was not permitted.

SPFX projects come prepackaged with a dependency on typescript 3.7.

To resolve this, the solution is to upgrade the build tools within SPFX projects:

npm remove @microsoft/rush-stack-compiler-3.7
npm install @microsoft/rush-stack-compiler-3.9 --save-dev

Additionally, it is necessary to adjust the tsconfig.json file to point to the correct build tools version:

{
  "extends": "./node_modules/@microsoft/rush-stack-compiler-3.9/includes/tsconfig-web.json",
  "compilerOptions": { .. }
}

The solution was discovered in the post Utilize Varied TypeScript Versions in SPFx Projects

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

Upgrading the import package alias from 'angular2' to '@angular'

Is there a way to update the package alias for import purposes? I am trying to utilize ng2-bootstrap, which requires @angular/core, but I have been referencing it as angular2/core. This mismatch is causing my screen to crash! ...

The element ''ag-grid-angular'' is unrecognized:

After using ag-grid successfully for a few months, I attempted to integrate a grid from a previous project into an Angular template like the one found in ngx-admin: ngx-admin This is how the template is structured: https://i.sstatic.net/Mfjw8.png I tr ...

The deletion by index feature seems to be malfunctioning in Angular

Is there a way to delete an item by its ID instead of just deleting the last element using this code? I want to create input fields with a delete button next to each one simultaneously. TS: public inputs: boolean[] = []; public addNew(): void { this ...

Steer clear of making changes to a JavaScript array

Here is my code snippet: let traces = { ref: null, min: null, max: null, avg: null }; let learning = { "Application": "b3", "t": [ { "d": 2, "BinaryType": "Current" }, { "d": 3, ...

Best practice for refreshing React UseState

Is there a more efficient way to rerender a component without using a resource-intensive script? I currently use a useState() to trigger the rerender, but it's not ideal. My current approach: const restartClick = (): void => { setDivClass(&q ...

Obtaining the current row index in React MUI Data Grid using React-Context

Scenario In my application, I have implemented an MUI Data Grid with custom components in each row: RowSlider, RowDate, and RowLock using the MUI Components Slider, Date Picker, and Button respectively. View the Data Grid Visualization The Slider and Da ...

The FirebaseX Ionic native plugin received 2 arguments instead of the expected 3-4

Trying to implement Firebase Phone Auth with the FirebaseX plugin, I encountered an issue. Here is the code snippet I used: async getVerificationCode(): void { const res:any = await this.firebaseX.verifyPhoneNumber('+16505553434', 60); ...

Challenges arise when dealing with generics in TypeScript

I'm a beginner in TypeScript and I'm trying to write a method with a generic type argument similar to what you can do in .Net. Here's the code snippet I've been working on: class TestObject { Id: number; Truc: string; Machin: str ...

Typescript with Angular: Despite having 7 values in the map, Map.get is returning undefined

Why does Map.get always return undefined when using a number from a form element (extra1) in this code snippet? extraById = new Map<number,Extra>(); @Input() extra1: number = -1; formChanged(carConfigurationFormChanged : any) { const index ...

Exploring the process of retrieving token expiration in TypeScript using the 'jsonwebtoken' library

Currently utilizing jsonwebtoken for token decoding purposes and attempting to retrieve the expiration date. Encountering TypeScript errors related to the exp property, unsure of the solution: import jwt from 'jsonwebtoken' const tokenBase64 = ...

What is the best way to implement Infinite scroll alongside Virtual scroll in Ionic 3?

Having recently delved into the world of Ionic and Angular, I am encountering some difficulties with implementing Infinite scroll alongside Virtual scroll. Despite pushing data such as images, text, and click functions from TypeScript, only empty Ionic car ...

Typescript's intriguing concept of objects containing arrays inside other objects

I have a structure similar to this and I am trying to create definitions for types/interfaces, but I am facing issues in making it work correctly. layoutsSet: { 1: { "lg": [ { i: "1", x: 0, ...

Using .on('mouseover', d => ..) does not yield the same `d` value as using .attr('foo', d => ..) in D3.js

I'm facing an issue with a mouseover tooltip in Observable that seems to fail when I transfer it to a Grafana plugin using React, D3, and Typescript. The technique I followed can be found in this article: Link to Article To simplify the code and deb ...

Missing from the TypeScript compilation are Angular5's polyfills.ts and main.ts files

Here is the structure of my Angular5 project. https://i.stack.imgur.com/tmbE7.png Within both tsconfig.app.json and package.json, there is a common section: "include": [ "/src/main.ts", "/src/polyfills.ts" ] Despite trying various solu ...

Show the attribute of an element in a pop-up window

I have a modal from ngx-bootstrap that I want to display in there a property of my object let's say the name: public students = [ { id: 1, name: 'lorem'} In this button that is common for all entries in the table (each row has this butt ...

Managing data, functions, and tasks consistently in an Electron-based React application despite navigating through various components - how to do it effectively?

As a newcomer to React, I have encountered several challenges while trying to implement certain features. Currently, I am working on an application that consists of a grid view and details view. Initially, I used props and functions to manipulate the state ...

Angular 4 - Automatically scroll to specific list item based on search query input

While I can achieve this with custom JavaScript, I'm curious if Angular 4 has any built-in features that could help. I have a list of checkboxes that are scrollable and a search input above them. My goal is to enable users to quickly jump to a section ...

What are the steps to implement the `serialport` library in `deno`?

After tinkering with Deno to extract readings from an Arduino, I encountered a roadblock when it came to using the serialport library correctly. Here is what I attempted: According to a post, packages from pika.dev should work. However, when trying to use ...

Issue with retrieving properties in Angular template due to undefined values in HTML

As a newbie to Angular, I am dedicated to improving my skills in the framework. In my project, I am utilizing three essential files: a service (Studentservice.ts) that emits an observable through the ShowBeerDetails method, and then I subscribe to this ob ...

Is it possible to create a VueJS 3 application in a unified ES Module bundle format?

In my Vue3 project, I have configured it with typescript and a main.ts entry file that has a single default export. import { App, createApp } from "vue"; import { createIntl } from "vue-intl"; import Application from "./App.vue&qu ...