Ways to expand the nested object in an interface: A practical example using MUI theme

I've customized a Material-UI theme and I'm trying to incorporate an extra color into the palette.

Here's how my initial custom theme is structured:

import { ThemeOptions } from "@mui/material/styles";

export const themeOptions: ThemeOptions = {
  palette: {
    primary: {
      main: colors.primary,
    },
    secondary: {
      main: colors.secondary,
    },
    background: {
      default: colors.bg,
      paper: colors.bg,
    },
  },
};

When I attempt to add the new color "muted" to the palette, I am required to update the type as well.

To achieve this, I have introduced a CustomThemeOptions interface. However, I am facing difficulties in getting it to function properly.

Below is my latest approach:

import { ThemeOptions } from "@mui/material/styles";

interface CustomThemeOptions extends ThemeOptions {
  palette: {
    muted: {
      main: string
    }
  }
}

export const themeOptions: CustomThemeOptions = {
  palette: {
    primary: {
      main: colors.primary,
    },
    secondary: {
      main: colors.secondary,
    },
    background: {
      default: colors.bg,
      paper: colors.bg,
    },
    muted: {
      main: colors.muted,
    }
  },
};

However, the extension of the interface overrides the original palette definition causing the primary and secondary fields to be rejected.

Answer №1

Ensure to expand nested fields:

interface CustomThemeOptions extends ThemeOptions {
  palette: ThemeOptions["palette"] & {
    muted: {
      main: string
    }
  }
}

Playground Link

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

Exploring the Children Property in TypeScript and the Latest Version of React

Within my App.tsx file, I am passing <Left /> and <Right /> as components to an imported component named <SplitScreen />. It seems that in React 18, the "children" prop needs to be explicitly typed. When I type it as React.Element[], eve ...

A Fresh Approach for Generating Unique UUIDs without Bitwise Operators

To generate UUIDs in TypeScript, I have implemented a method inspired by the solution provided on Stack Overflow. The code effectively converts JavaScript to TypeScript. generateUUID(): string { let date = new Date().getTime(); if (window.performa ...

What is the best way to implement CSS properties on Material UI components?

I've recently started exploring Material UI, but I'm having trouble understanding how the spacing properties function. I'm trying to utilize the "spacing" feature for various elements, but it appears that it only works for "Box" components a ...

"Navigating through events with confidence: the power of event

Imagine I am developing an event manager for a chat application. Having had success with event maps in the past, I have decided to use them again. This is the structure of the event map: interface ChatEventMap { incomingMessage: string; newUser: { ...

Using the expect statement within a Protractor if-else block

My script involves an if-else condition to compare expected and actual values. If they do not match, it should go to the else block and print "StepFailed". However, it always executes the if block and the output is "step passed" even when expected does not ...

Customize the focus and selected background color of Material UI Select component

I am trying to customize the background color of a Select component and its MenuItems. Specifically, I want to remove or override the default background color of the focused Select component and selected MenuItem. The current background color of the selec ...

Deduce the argument type of a class from the super call

I'm currently working on a project utilizing the discord.js library. Within this project, there is an interface called ClientEvents which contains different event argument tuple types: interface ClientEvents { ready: []; warn: [reason: string] m ...

Evaluating file selection and uploading functionality within Spectron

Currently, I am faced with the challenge of writing a test for an electron GUI that includes a choose file dialog. Unfortunately, I do not have access to the inner workings of the GUI. Here is the code snippet I have written: await app.client.chooseFile( ...

Guidelines for implementing more rigorous type checks in TypeScript:

I am looking to enhance the code validation process and eliminate any implicit 'any' types. To achieve this, I have set "strict": true in my tsconfig.json. { "compilerOptions": { "target": "ES5", ...

Error TS2339: Cannot access attribute 'reactive_support' on interface 'LocalizedStringsMethods'

Encountering the error TS2339: Property 'reactive_support' does not exist on type 'LocalizedStringsMethods' I recently updated TypeScript from version 2.6 to 2.9, and attempted import LocalizedStrings from 'react-localization&apo ...

Jest assertions encountering type errors due to Cypress

After using react-testing-library and @testing-library/jest-dom/extend-expect, I decided to install Cypress. However, I now face Typescript errors on all my jest matchers: Property 'toEqual' doesn't exist on type 'Assertion'. Did ...

Clear Material-UI Autocomplete Selected Value

I'm facing an issue with my React code. Using Material-UI and Redux-Form, I have a select input like and after changing this select, I need to reset the value in . I've tried using the 'change' action from redux-form to set the value ...

What are the steps to tackle the Element Type Invalid error in React?

I am a beginner in using react. The code provided is functioning correctly in the tutorial but encountering errors when implemented. Can you please review this code? import React from 'react'; import './SidebarOption.css'; ...

obtain the content of a TextField element

In my React component that utilizes MaterialUI, I have created a simple form with a text field and a button: export default function AddToDo() { const classes = useStyles(); return ( <div style={{ display: "flex" }} ...

Elements constrained by themselves in a rest parameter with generic types

When using Typescript, it is possible to infer tuple types for generic rest parameters that are constrained by an array type. However, in my specific case, this functionality does not seem to work as expected. I am attempting to pass a series of pairs co ...

What is the best way to incorporate the C# in keyword into TypeScript?

When coding in C#, I often utilize the in keyword within conditional statements. if (1.In(1, 2) I am curious about how to implement the IN keyword in typescript. ...

While attempting to update the package.json file, I encountered an error related to the polyfills in Angular

I have been working on a project with ng2 and webpack, everything was running smoothly until I updated the package.json file. Since then, I have been encountering some errors. Can anyone please assist me in identifying the issue? Thank you for any help! P ...

Unable to incorporate .tsx files into a Node.js Web Application project

After creating a new Node.js Web Application in Visual Studio 2015, I encountered an issue with adding and compiling .tsx files to the project. Instead of being added to the actual project, the .tsx file was placed into a Virtual Project. The project is co ...

Angular 2 signal sender

I have a specific class definition for my Project: export class Project { $key: string; file: File; name: string; title: string; cat: string; url: string; progress: number; createdAt: Date = new Date(); constructor(file: File) { th ...

A guide to integrating cell updates with dropdown selection in the Material-UI Data Grid

When I need to update cells in a column, I usually use 'type: singleSelect' and 'valueOptions' in the Data Grid column definition. const columns = [ { field: "id", headerName: "ID", sortable: ...