Building a Model Class with redux-orm and TypeScriptCreating a new Model Class with

I've been successfully using redux-orm with JavaScript, however, I'm facing issues when trying to convert my code to TypeScript.

Even though I have already implemented the render() method in my Folder Model Class, the TypeScript transpiler is showing an error message:

Property 'name' does not exist on type 'Folder'

How can I inform TypeScript that the name property exists in instances of the Folder class?

interface IFolder{
    id: number;
    name: string;
    parent: any;
}

export class Folder extends Model<ModelFields, IFolder>{

    static get modelName() {
        return 'Folder';
    }

    get render(){
        return (<p>{this.name}</p>)
    }

    static get fields() {
        return {
            id: attr(),
            name: attr(),
            parent: fk('Folder', 'children')
        };
    }
}

Answer №1

If you want to avoid repeating all instances of IFolder within class Folder, you can utilize the following trick:

export class Folder extends Model<ModelFields, IFolder> implements IFolder {
    id: number;
    name: string;
    parent: any;
...

Alternatively, you could use this workaround to prevent duplicating code:

class IFolder extends Model<ModelFields, IFolder> {
    id: number;
    name: string;
    parent: any;
}

export class Folder extends IFolder {

Answer №2

When Babel transpiles code, it adds extra property descriptors to the prototype chain of the Model, causing conflicts with existing descriptors set by redux-orm during schema registration. This is why the "Maximum call stack size exceeded" error occurs.

@types/redux-orm for version redux-orm:0.13 functions differently.

The provided source code lacks the definition of ModelFields, so we will assume that the expected fields consist of:

  • id - attribute
  • name - attribute
  • parent - self-referencing foreign key

Here's an example:

interface FolderFields {
  id: number;
  name: string;
  parent: Folder
  children: QuerySet<Folder>
}

export class Folder extends Model<typeof Folder, FolderFields> {
    static modelName = 'Folder' as const;
    static fields = {
        id: attr(),
        name: attr(),
        parent: fk('Folder', 'children')
    }
}

In redux-orm version 0.14, it is anticipated that types will use declaration merging to facilitate cleaner Model definitions.

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

Error message: "Context is being utilized outside of the provider - nextjs 13"

Transitioning from nextjs 12 to nextjs 13 has presented some challenges, particularly with the new app/ folder. I encountered an error when attempting to launch the application using the login/ route: Unhandled Runtime Error Error: context used outside of ...

Learn how to easily set a radio button using Angular 4 and JavaScript

It seems like a simple task, but I am looking for a solution without using jQuery. I have the Id of a specific radio button control that I need to set. I tried the following code: let radiobutton = document.getElementById("Standard"); radiobutton.checke ...

The deletion of property '1' from the [object Array] is not possible

How to Retrieve a List in My Components Using Input : @Input() usersInput: Section[]; export interface Section { displayName: string; userId: number; title: number; } Here is the Value List : 0: displayName: "بدون نام" ...

Customize the date format of the Datepicker in Angular by implementing a personalized pipe

I am dealing with a datepicker that defaults to the MM/dd/yyyy format, and I need it to adjust based on the user's browser language. For example, if the browser language is English India, then the format should be set to dd/MM/yyyy as shown below. Be ...

Is utilizing React's useEffect hook along with creating your own asynchronous function to fetch data the best approach

After attempting to craft a function for retrieving data from the server, I successfully made it work. However, I am uncertain if this is the correct approach. I utilized a function component to fetch data, incorporating useState, useEffect, and Async/Awa ...

React Material-UI background image not covering entire screen

Currently, I am working on a login page that includes a MUI Container component. One of my objectives is to add a background image to the entire page while utilizing react router. To achieve this, I have applied styling to the outer div as shown below: & ...

Adding an exception for ClickAwayListener in React-MUI

Hey there! I'm currently exploring MUI and trying to incorporate the ClickAwayListener API into my project, but I'm facing some difficulties. You can take a look at my project on codesandbox here: https://codesandbox.io/s/react-leaflet-icon-ma ...

When working with TextareaAutosize component in MUI, an issue surfaces where you need to click on the textarea again after entering each character

One issue that arises when using TextareaAutosize from MUI is the need to click on the textarea again after entering each character. This problem specifically occurs when utilizing StyledTextarea = styled(TextareaAutosize) The initial code snippet accompl ...

Steps to incorporate multiple line selections in a select dropdown using Material UI

I am trying to create a Material UI Select where the options (MenuItem's) can accommodate long texts. For example, here is what one option may look like: "A new building or one that has undergone a major renovation within the last two years, which on ...

React component state remains static despite user input

I am currently in the process of developing a basic login/signup form using React, Typescript (which is new to me), and AntDesign. Below is the code for my component: import React, { Component } from 'react' import { Typography, Form, Input, But ...

Leveraging nginx for serving dynamic content

I recently utilized nginx to host my node.js application. After creating a build of the app, I configured the root directory in my nginx.conf file to point to the location of the build folder. It was successful, and my application ran smoothly on nginx. H ...

Show the React component upon clicking the Add button

I recently developed a React component that reveals a new section whenever the user clicks the "New Section" button. However, I have encountered an issue - I would like the component to display multiple sections based on how many times the button is clic ...

Error encountered when trying to start ReactJS with npm. Issue with the project's dependency tree

Error in Terminal npm start > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f39e9287909b8087928789b3c3ddc2ddc3">[email protected]</a> start C:\Users\07\OneDrive\Desktop\matchstatzta ...

Striving to implement a dynamic expand and collapse animation feature for a card in ReactJS

I'm attempting to create an expand and collapse animation effect on a card without relying on bootstrap or any external libraries. I have tried adding a transition property but it doesn't seem to work when the button is clicked. Here is the comp ...

Having trouble downloading files in React and Node.js

I need to retrieve a specific file from the uploads folder and download it into my download folder. Both folders are located in the same directory, and the file's name is BasicUserFile-id.jpg. On my second attempt, I tried hardcoding the file name bu ...

Is there a way to conceal 'private' methods using JSDoc TypeScript declarations?

If we consider a scenario where there is a JavaScript class /** * @element my-element */ export class MyElement extends HTMLElement { publicMethod() {} /** @private */ privateMethod() {} } customElements.define('my-element', MyElement) ...

Are there any possible resolutions to the issues plaguing Next.Js?

After switching from React to Next.JS, I've encountered some challenges that I'm hoping to find solutions for: In contrast to React, Next.JS doesn't allow me to change a specific part of a webpage and maintain a static element like a navb ...

My React app is facing challenges in production due to issues with webpack

To enhance my React application, I incorporated a Google Maps API key and a mail sending library. However, safeguarding sensitive information such as the API key and password is crucial, which cannot be stored in simple variables. To secure these sensitiv ...

Encountering a problem with React components displaying incorrect sizes while utilizing flexbox styling

I am creating a basic test application using NextJS/React. Take a look at the code snippet below: The content of Board.tsx file: import './Board.css'; export default function Board() { return <div className="board"> < ...

An issue occurred while attempting to retrieve Firebase data using an HTTP GET request

When trying to retrieve my data from firestore using an HTTP get request, I encountered an error. It might be helpful if my data in firestore was stored in JSON format. I'm not sure if this is feasible. <!DOCTYPE html> <html lang="en"> ...