mui / mui/material-ui

[question] TypeScript error with theme options

Open
#37,148 2 comments 0 reactions 1 assignee View on GitHub

@siriwatknp is already working on this.

Since May 3, 2023.

customization: theme scope: system support: question typescript
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

Order ID or Support key 💳 (optional)

48888

Duplicates
  • I have searched the existing issues
Latest version
  • I have tested the latest version
The problem in depth 🔍

I am getting a TypeScript error saying that my options object is not assignable to parameter type Themeoptions | undefined.

What do I need to do to fix this error?

Here is my theme.ts file:

import {blue, grey, red} from "@mui/material/colors";
import {createTheme} from "@mui/material/styles";
import * as React from "react";

const primaryColor = blue;
const secondaryColor = red;

declare module '@mui/material/styles' {
    interface TypographyVariants {
        bodyLandingPage1: React.CSSProperties;
    }

    // allow configuration using `createTheme`
    interface TypographyVariantsOptions {
        bodyLandingPage1?: React.CSSProperties;
    }
}

// Update the Typography's variant prop options
declare module '@mui/material/Typography' {
    interface TypographyPropsVariantOverrides {
        bodyLandingPage1: true;
    }
}


const theme = createTheme({
    breakpoints: {
        values: {
            xs: 0,
            sm: 600,
            md: 900,
            lg: 1200,
            xl: 1536,
        },
    },
    palette: {
        primary: {
            50: primaryColor[50],
            main: primaryColor[700],
            800: primaryColor[800],
        },
        secondary: {
            50: secondaryColor[50],
            main: secondaryColor[700],
        },
        grey: {
            50: grey[50],
            100: grey[100],
            200: grey[200],
            300: grey[300],
            400: grey[400],
            500: grey[500],
            600: grey[600],
        },
    },
    typography: {
        bodyLandingPage1: {
            fontSize: "18px",
            fontFamily: 'Roboto, sans-serif'
        }
    }
});


export default theme;

tsconfig.json:

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "module": "ESNext",
    "skipLibCheck": true,

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true
  },
  "include": ["src"],
  "references": [{ "path": "./tsconfig.node.json" }]
}
Your environment 🌎
`npx @mui/envinfo`
 System:
    OS: Windows 10 10.0.22621
    @mui/icons-material: ^5.11.16 => 5.11.16
    @mui/material: ^5.12.2 => 5.12.2
    @mui/private-theming:  5.12.0
    @mui/styled-engine:  5.12.0
    @mui/system:  5.12.1
    @mui/types:  7.2.4
    @mui/utils:  5.12.0
    @types/react: ^18.0.28 => 18.2.0
    react: ^18.2.0 => 18.2.0
    react-dom: ^18.2.0 => 18.2.0
    typescript: ^5.0.2 => 5.0.4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.