Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript cannot name the type of an exported MUI styled component

ErrorThe inferred type of 'X' cannot be named without a reference to '...'. This is likely not portable. A type annotation is necessary.

Short answer

TypeScript must write the component's type into a .d.ts file, but that type lives in a package you did not import directly. The fix is to annotate the exported value with an explicit type, or to list the package that owns the type as a direct dependency in package.json.

  • Applies to TypeScript 5.5+, @mui/material 6, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Confirm that declaration output is what triggers the error, and read which package the message names.

    npx tsc --declaration --emitDeclarationOnly --outDir dts-check
  2. 2Match what you see, then jump to its fix

    1. 1The error names the exported const, for example export const StyledCard = styled(Card)(...)The exported value has no type annotationFix
    2. 2The message names a path through node_modules of a package you never import yourselfThe package that owns the type is only an indirect dependencyFix

What does the error look like?

  • The inferred type of 'X' cannot be named without a reference to '...'. This is likely not portable. A type annotation is necessary.
  • error TS2742

Cause 1 of 2

The exported value has no type annotation

1Check

The TypeScript team says an explicit type annotation on the flagged node is the main answer, and the error message itself recommends it.

grep -rn "export const .* = styled(" src/

2Fix

Give the export a type. A simple route is to type it as the component you wrapped.

import { styled } from '@mui/material/styles';
import Button from '@mui/material/Button';

export const CustomButton: typeof Button = styled(Button)({
  // styles
});

3Verify

Run the declaration check again. TS2742 is gone.

npx tsc --declaration --emitDeclarationOnly --outDir dts-check

Confirmed by github.com

Did this fix it?

Cause 2 of 2

The package that owns the type is only an indirect dependency

1Check

A declaration file needs a type from an indirect dependency, and TypeScript cannot always find a path to it. Look at which package the message names and whether it is in your package.json.

npm ls @mui/types @emotion/styled

2Fix

Declare that package as a direct dependency. The TypeScript team notes that from 5.5, the names can be generated when the dependencies are direct.

npm install @emotion/styled @emotion/react

3Verify

Run the declaration check again.

npx tsc --declaration --emitDeclarationOnly --outDir dts-check

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Annotate the exported values of any library or shared package that emits .d.ts files.
  • List every package whose types leak into your public API as a direct dependency.
  • Keep one copy of each types package; check with npm ls after upgrades.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.