Skip to content

Improve Typescript DX when using createTheme (Suggestion) #45690

Open
@rob2d

Description

@rob2d

Summary

This is a very minor request as I already do this locally but I'm working on a new Mui6 project, and noticed that there is no concrete type/interface for the parameter accepted by createTheme and it is also a bit of an amalgamation.

Simplest workaround would be to read the first param of the factory method (would not be the most obvious to new TS devs):

export type MuiUserThemeDef = Parameters<typeof createTheme>[0]

^ /feel free to ignore the naming. It's just for quick demonstration

We could then have that standalone type without inference @ MUI; stored with UI code vs nestled into the build.

Image

I have found that cross referencing theme variables has been a hassle in previous version before TS when figuring out what is customizable and what isn't quickly -- which is where the context of suggestion came from. It is a little bit more complex in MUI6 now that we are not just working with BaseTheme directly as a root input to hand roll those and then provide as input without typing.

Examples

In the doc it is suggested for PigmentCSS to have createTheme declared in the config, but it is not exactly clean for non-trivial theming and also mixing concerns to attach the theme colors/vars themselves into the core build setup.

Image
^ from MUI's setup instructions

Motivation

Separating concerns between declaring themes and build configurations as a norm, and having an explicit non-inlined type out of the box. This is just feedback/a suggestion as I am ok to just provide this myself in my codebase.

Search keywords: createTheme typescript type

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions