Skip to content

[theme][typescript] Wrong types when using CSS Vars #49474

Closed

Description

@olivier-lxo

Steps to reproduce

Steps:

  1. Open this link to live example: https://stackblitz.com/edit/github-xpjupurj?file=src%2FApp.tsx
  2. Notice the type on theme.vars.zIndex.appBar is number but should be string

Current behavior

The type on theme.vars.zIndex.appBar is number

Expected behavior

The type on theme.vars.zIndex.appBar should be string

Context

No response

Your environment

npx @mui/envinfo
 System:
    OS: Linux 7.2 Fedora Linux 44 (Workstation Edition)
  Binaries:
    Node: 24.22.0 - /home/olivier/.local/share/vite-plus/js_runtime/node/24.22.0/bin/node
    npm: 12.0.2 - /home/olivier/.local/share/vite-plus/package_manager/npm/12.0.2/npm/bin/npm
    pnpm: Not Found
  Browsers:
    Chrome: canary
    Firefox: 158.0
    Vivaldi: 152
  npmPackages:
    @base-ui/utils:  0.3.2
    @emotion/react:  11.14.0
    @emotion/styled:  11.14.1
    @mui/core-downloads-tracker:  9.4.0
    @mui/icons-material: 9.4.0 => 9.4.0
    @mui/lab: 9.0.0-beta.9 => 9.0.0-beta.9
    @mui/material: 9.4.0 => 9.4.0
    @mui/private-theming:  9.4.0
    @mui/styled-engine:  9.4.0
    @mui/system:  9.4.0
    @mui/types:  9.4.0
    @mui/utils:  9.4.0
    @mui/x-charts: 9.13.0 => 9.13.0
    @mui/x-charts-vendor:  9.13.0
    @mui/x-data-grid:  9.13.0
    @mui/x-date-pickers:  9.13.0
    @mui/x-internal-gestures:  9.13.0
    @mui/x-internals:  9.14.0
    @mui/x-virtualizer:  0.7.1
    @types/react: 19.3.0 => 19.3.0
    react: 19.3.0 => 19.3.0
    react-dom: 19.3.0 => 19.3.0
    typescript: ^6.0.0 => 6.0.3

Search keywords: css vars, theme, types

Activity

  1. lazerg commented on Oct 16, 2026

    @lazerg
    Contributor

    It is a bug. ThemeVars in createThemeFoundation.ts reuses the regular ZIndex interface, where everything is a number. But zIndex isn't excluded in shouldSkipGeneratingVar, so at runtime each theme.vars.zIndex.* is a string like 'var(--mui-zIndex-appBar, 1100)'.

    theme.vars.opacity has the same problem. It's typed with the numeric Opacity interface but holds strings like 'var(--mui-opacity-inputPlaceholder, 0.42)'. I checked both with createTheme({ cssVariables: true }) on 9.4.0. theme.vars.shape.borderRadius is fine, it's already typed number | string.

    Module augmentation won't get you around this, since merging interfaces can't change the type of a property that already exists. Until the types are fixed, use theme.zIndex.appBar if you need the actual number, or theme.vars.zIndex.appBar as unknown as string where you want the CSS variable.

  2. olivier-lxo commented on Oct 16, 2026

    @olivier-lxo
    ContributorAuthor

    Thank you. Indeed, more number properties that have a css variable value have that problem.

    The as unknown as string workaround does work. The actual problem is that you need to be aware of the wrong type as code will happily compile while being buggy otherwise.

  3. changed the title [-]Wrong types when using CSS Vars[/-] [+][theme][typescript] Wrong types when using CSS Vars[/+] on Oct 19, 2026
  4. github-actions commented on Oct 19, 2026

    @github-actions
    Contributor

    This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
    Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.


    @olivier-lxo Thanks again for creating this issue! If you have a moment, we would love to hear your thoughts on how we handled it with this short feedback form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions