18

I create a Next.js application that use Material-UI and @react-three/fiber library.

I recently pass Material-UI to V5 and a error is appear.

Here is the error: error

I search everything to remove it but I found nothing.

So I try to create a new project with Material-UI V5 and I install other dependency one by one. In the begin the error is not show but when I install @react-three/fiber AND @react-three/drei the error appear.

So I found that a Box component is exported from @material-ui and @react-three/drei so I understand now the error.

Here is my package.json:

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@emotion/react": "^11.4.0",
    "@emotion/styled": "^11.3.0",
    "@material-ui/core": "^5.0.0-beta.2",
    "@material-ui/styles": "^4.11.4",
    "@react-three/fiber": "^7.0.6",
    "next": "11.0.1",
    "react": "17.0.2",
    "react-dom": "17.0.2"
  },
  "devDependencies": {
    "@types/react": "17.0.16",
    "eslint": "7.32.0",
    "eslint-config-next": "11.0.1",
    "typescript": "4.3.5"
  }
}

My tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "src",
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

But I don't found how to fix it...

Someone have idee?

Melvynx
  • 912
  • 1
  • 6
  • 20

5 Answers5

17

Solution 1

The stable version of MaterialUI fix this problem.

Use this package:

npm install @mui/material

Solution 2 by Vivere

Add the component props to a div to avoid TypeScript error.

<Box component="div">...</Box>
Melvynx
  • 912
  • 1
  • 6
  • 20
  • I ran into this error too. I understand that both `@mui/material` and the three libraries export a `Box`, but why should this error appear? – Vivere Oct 19 '21 at 06:08
  • I think it's maybe bcs types definitions in @material-ui/core v.5 is not correctly defined and enter in conflict with @three.js definition. And when you import both, typescript doesn't know which types is correct. Did you try with `@mui/material` ? – Melvynx Oct 19 '21 at 09:13
  • Yes, I'm using the latest version of `@mui/material`. I see that for you that fixed it but in my case the issue is still persisting. – Vivere Oct 19 '21 at 09:17
  • It's the same error? When you remove `drei` package the error is no longer here? – Melvynx Oct 19 '21 at 09:20
  • Yes, exactly the same. I tested and removed the `drei` and the error goes away. – Vivere Oct 19 '21 at 09:30
  • Can you share a simple codesandbox to compare with my own codebase? I try now to import Box from mui and Box from three and the error doesn't appear – Melvynx Oct 19 '21 at 09:37
  • I don't have a single clue. On my local pc, it is working, on the remote one, it is not. I create a code sandbox, there it is working as well. I even reinstalled node, deleted the cache, removed node_modules & package JSON and the error still persists. Could you maybe share your `package.json` versions? – Vivere Oct 19 '21 at 11:11
  • Ok, I found a way to reproduce it. Apparently, I need to also write some three code, and if the components are at the same level, then the error appears – Vivere Oct 19 '21 at 13:54
  • 1
    Ok nice. You're on the good way! Here is my package.json: https://gist.github.com/Melvynx/f1b1402411d1d7ef1d47c58d90cebfac – Melvynx Oct 20 '21 at 09:49
  • 5
    So I managed to solve it by writing the component type of the box: ` – Vivere Oct 20 '21 at 10:55
  • 1
    After hours of looking, looking again, scratching my head, renaming modules, reading webpack docs, reading typescript articles, github issue logs, code-splitting, ad nauseum, @Vivere your last note fixed this ... thank you! ... I should note, this is also an issue using Babylon ... This will do for now, but there HAS to be a better fix than this ... – Paul Nelligan Nov 29 '21 at 11:24
  • 1
    I have added your comment in my answer @Vivere – Melvynx Nov 29 '21 at 12:59
8

I had the same error because of importing @react-three/drei. I have successfully localized files, which cause the error to appear, but i'm unable to determine the root cause. I would appreciate all input from more TypeScript-savvy people out there.

This is a rather ugly workaround. Delete all of these definition files:

  • node_modules/@react-three/drei/core/
    • MeshWobbleMaterial.d.ts
    • MeshDistortMaterial.d.ts
    • PointMaterial.d.ts (<- for newer versions)

Of course now you can't use these Components in a Typescript context now.

You can share and pin these changes by using patch-package. This will setup a post install step, that will apply the changes (deleted files) after running npm/yarn install.

Inuniku
  • 216
  • 3
  • 6
  • Thanks, but if you redo a `npm install` the deleted modules will be here again? – Melvynx Oct 20 '21 at 19:25
  • 1
    This will be solved by `patch-package`. When you put `"postinstall": "patch-package"` to your package.json, after downloading packages during `npm install`, this script will delete the .d-files if present. Please look into the discription of patch-package, or ask me, when you need help! – Inuniku Oct 20 '21 at 21:53
  • THIS WORKED !!! this is so bad !! – Tanzeel Dec 29 '21 at 20:09
  • Can you share the process you used to localize the conflicting files? – Marcel Jan 13 '23 at 01:17
  • Thanks for the tip regarding `patch-package`. I used it to remove the real cause of the issue - the cavalier global extension of `JSX.IntrinsicElements` by the library, which causes heavily templated/inferred components such as `Box` to grow too big. See [my note on GitHub](https://github.com/pmndrs/react-three-fiber/discussions/1752#discussioncomment-6282948). – hon2a Jun 26 '23 at 14:51
5

If you're using VS Code


This worked for me (TLDR)

  1. create a .vscode directory and add the settings.json file.
  2. Add this configuration
    {
      "typescript.enablePromptUseWorkspaceTsdk": true
    }
    

Context

if you're using vs code and seeing this error. This was because of an inconsistent typescript version between what vscode internally uses to provide ts-related features and the typescript versions you're using in your app.

I tried this option

https://stackoverflow.com/a/74847054/13822685

but could not follow since the latest version of VS Code does not show the option to switch TS version to the workspace version in case you've never enabled this before. Below are the version details.

Version: 1.74.2 (user setup)
Commit: e8a3071ea4344d9d48ef8a4df2c097372b0c5161
Date: 2022-12-20T10:29:14.590Z
Electron: 19.1.8
Chromium: 102.0.5005.167
Node.js: 16.14.2
V8: 10.2.154.15-electron.0
OS: Windows_NT x64 10.0.19044
Sandboxed: No

So typescript.enablePromptUseWorkspaceTsdk will force the vscode to use the typescript version of the current workspace that is likely to fix this issue.

abdadeel
  • 446
  • 5
  • 8
  • I'm using VSCode and selecting the workspace typescript version solved the issue. (press F1 -> Typscript: Select Typescript Version) – sameera madushan Dec 30 '22 at 05:11
  • yes, it does solve the issue. But the problem in my case was; I was not even seeing the option `Select Typescript Version` in the command palette. After these configurations, I started seeing the option to select `Current Workspace Version` option. – abdadeel Dec 30 '22 at 10:20
  • Given your fix, it's likely that the bug is present only in certain versions of TS. What version are you using? :) – Alexis Delrieu Jan 05 '23 at 10:36
  • From the other post, you're using 4.7.4, but I still have the issue in 4.7.4 – Alexis Delrieu Jan 05 '23 at 10:57
  • Still facing this issue. Can't even set current workspace version even after restarting VSCode after adding the field to settings.json – Leafyshark Jan 05 '23 at 23:45
  • @Leafyshark are you seeing the option to switch to the current workspace version in your command pellete? – abdadeel Jan 06 '23 at 12:44
  • @abdadeel even after adding typescript.enablePromptUseWorkspaceTsdk: true to my settings.json, I do not see it. Only in the particular project I am seeing the error "expression produces a union type that is too complex to represent" - other projects without the error I am able to "use workspace version" – Leafyshark Jan 06 '23 at 13:23
1

I solved mine by adding this to tsconfig.json.

"jsx": "react",
"lib": ["ES5", "ES2015", "ES2016", "DOM", "ES2015.Promise"],
"module": "esnext",
"moduleResolution": "node",
Four
  • 734
  • 5
  • 9
-1

I've encountered this same issue with @react-three/fiber and chakra-ui

This is what worked for me. Cast the props as any. This may be a hacky way to do things but it does clear out the warning.

see sample code here:

import NextLink from 'next/link';
import { Link as CharkaLink, LinkProps } from '@chakra-ui/react';

export interface ICustomLinkProps extends LinkProps {
  href: string;
  external?: boolean;
}

const CustomLink = (props: ICustomLinkProps) => {
  const { href, children, color = 'teal.500' } = props;
  
// typecast to any to prevent issues with props from three/drei or three/fiber
  const propsAny = props as any;
  return (
    <NextLink href={href} passHref legacyBehavior>
      <CharkaLink color={color} {...propsAny}>
        {children}
      </CharkaLink>
    </NextLink>
  );
};

export default CustomLink;

ezennnn
  • 1,239
  • 11
  • 13