question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript issues with @material-ui/styles and @material-ui/core packages

See original GitHub issue
  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

When using the new @material-ui/styles and @material-ui/core together in the same project, I can successfully compile the project using the tsc compiler.

Current Behavior 😯

Receiving typescript errors.

node_modules/@material-ui/core/styles/createGenerateClassName.d.ts:1:10 - error TS2305: Module '"../../../../../../../../myProject/node_modules/jss/src"' has noexported member 'GenerateClassName'.

1 import { GenerateClassName } from 'jss';
           ~~~~~~~~~~~~~~~~~

node_modules/@material-ui/core/styles/jssPreset.d.ts:1:10 - error TS2724: Module '"../../../../../../../../myProject/node_modules/jss/src"' has no exported member 'JSSOptions'. Did you mean 'JssOptions'?

1 import { JSSOptions } from 'jss';
           ~~~~~~~~~~

node_modules/@material-ui/core/styles/withStyles.d.ts:32:15 - error TS2694: Namespace '"/myProject/node_modules/jss/src/index"' has no exported member 'CreateStyleSheetOptions'.

32   extends JSS.CreateStyleSheetOptions<ClassKey> {
                 ~~~~~~~~~~~~~~~~~~~~~~~

Found 3 errors.

Steps to Reproduce 🕹

I’ve attempted to replicate this in codesandbox and have been unable to. I think the reason for that is that it does not accurately represent a node_modules structure…hard to tell. I believe the core of this issue is we have 2 different versions of JSS in play:

`➜ yarn why jss yarn why v1.12.0 [1/4] 🤔 Why do we have the module “jss”…? [2/4] 🚚 Initialising dependency graph… [3/4] 🔍 Finding dependency… [4/4] 🚡 Calculating file sizes… => Found “jss@10.0.0-alpha.8” info Reasons this module exists

  • project#@material-ui#styles” depends on it
  • Hoisted from “project#@material-ui#styles#jss” info Disk size without dependencies: “180MB” info Disk size with unique dependencies: “896MB” info Disk size with transitive dependencies: “916MB” info Number of shared dependencies: 5 => Found “@material-ui/core#jss@9.8.7” info This module exists because “project#@material-ui#core” depends on it. info Disk size without dependencies: “408MB” info Disk size with unique dependencies: “544MB” info Disk size with transitive dependencies: “632MB” info Number of shared dependencies: 5 `

I believe since core uses one version and styles another, combined with the install() mechanism at runtime (which TypeScript presumably cant know about since its runtime), its impossible to the new styles package alongside core.

To trigger the error, you can try to compile a file with imports like this:

import React, { FC } from 'react'
import { ThemeProvider as MuiThemeProvider } from '@material-ui/styles'
import createMuiTheme from '@material-ui/core/styles/createMuiTheme'

interface Props {
  children: any
  theme: any
  /** The theme to apply to all children. */
}

const ThemeProvider: FC<Props> = props => {
  return (
    <MuiThemeProvider theme={createMuiTheme(props.theme)}>
      {props.children}
    </MuiThemeProvider>
  )
}

export default ThemeProvider

Context 🔦

Your Environment 🌎

Tech Version
Material-UI v3.9.0
React v16.7.0
Browser N/A
TypeScript v3.2.2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
eps1loncommented, Jan 30, 2019

What is working for me is moving node_modules/@types/jss (which is the version for v9) to node_modules/@material-ui/core/node_modules/@types/jss. This way Typescript will correctly use the v9 typings for @material-ui/core and the v10 typings for @material-ui/styles.

You could do this with e.g. a postinstall entry in your package.json (unix only):

"postinstall": "mkdir -p node_modules/@material-ui/core/node_modules/@types/jss && cp -r node_modules/@types/jss node_modules/@material-ui/core/node_modules/@types"

Be aware that this is heavily dependent on the package manager you use (and the node_modules/ layout).

5reactions
lmachenscommented, Feb 8, 2019

If someone needs a node solution for the workaround:

jss-types-workaround.js

const fs = require('fs');
const path = require('path');

const copyFolderSync = (from, to) => {
  fs.mkdirSync(to, { recursive: true });
  fs.readdirSync(from).forEach(element => {
    if (fs.lstatSync(path.join(from, element)).isFile()) {
      fs.copyFileSync(path.join(from, element), path.join(to, element));
    } else {
      copyFolderSync(path.join(from, element), path.join(to, element));
    }
  });
};

copyFolderSync('node_modules/@types/jss', 'node_modules/@material-ui/core/node_modules/@types/jss');

package.json

"scripts": {
  "postinstall": "node jss-types-workaround"
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript issues with @material-ui/styles and ... - GitHub
TypeScript issues with @material-ui /styles and @material-ui/core packages # ... its impossible to the new styles package alongside core.
Read more >
TypeScript - Material UI - MUI
Complications with the component prop​​ Because of some TypeScript limitations, using the component prop can be problematic if you are creating your custom ......
Read more >
Troubleshooting - Material UI - MUI
This document covers known issues and common problems encountered when migrating from Material UI v4 to v5.
Read more >
Migrating to v5: getting started - Material UI - MUI
The minimum supported version of TypeScript has been increased from v3.2 to v3.5. We try to align with types released by DefinitelyTyped (i.e....
Read more >
TypeScript - Material-UI
Unfortunately due to a current limitation of TypeScript decorators, withStyles(styles) can't be used as a decorator in TypeScript. Customization of Theme. When ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found