24

My project's setup is tsdx (based on Rollup and Typescript).
Working in my IDE (vscode) everything looks fine, and even running yarn tsc works without errors.

When I'm running yarn build (which is tsdx build) I get the following error:

(typescript) Error: /home/me/dev/app/src/components/alert/Alert.tsx(36,7): semantic error TS2742: The inferred type of 'AlertContainer' cannot be named without a reference to '@emotion/serialize/node_modules/csstype'. This is likely not portable. A type annotation is necessary.
Error: /home/me/dev/app/src/components/alert/Alert.tsx(36,7): semantic error TS2742: The inferred type of 'AlertContainer' cannot be named without a reference to '@emotion/serialize/node_modules/csstype'. This is likely not portable. A type annotation is necessary.

The code referenced in the error is:

type AlertContainerProps = {
  theme: any
};
const AlertContainer = styled(animated.div)<AlertContainerProps>`
  ${(props) => props.theme.primaryView}
  ...
`;

...

type AlertContentProps = Pick<React.ComponentProps<typeof AlertContainer>, 'style'> & {
  status?: string
};

What am I doing wrong? How can I fix it?
I tried this solution but it didn't work.

2
  • Did you find any solutions about this ? Commented May 6, 2022 at 8:51
  • No, I have just types it as any Commented May 7, 2022 at 9:05

4 Answers 4

11

This issue occurs when you are exporting a function whose return type could not be inferred by TypeScript

For example, consider the code snippet

const foo = async () => {
  const data = someExternalLibary.doStuff();
  return data;
};

If TypeScript throws the following error:-

The inferred type of `foo` cannot be named without a reference to `../node_modules/someExternalLibary`. This is likely not portable. A type annotation is necessary.

this means that TS is saying that someExternalLibary.doStuff()'s return type could not be inferred because the return type of doStuff is not exported by someExternalLibrary library


So what's solution? just add the type annotation to data. Say, required type annotated is DoStuffReturnType

const foo = async () => {
  const data: DoStuffReturnType = someExternalLibary.doStuff(); // Add type annotation to `data`
  return data;
};

Now you should be good to go!

Sign up to request clarification or add additional context in comments.

Comments

10

This helped me

import { StyledComponent } from '@emotion/styled';
import type { BoxProps } from '@mui/material/Box';
import Box from '@mui/material/Box';
import { styled } from '@mui/material/styles';

export const BoxStyled: StyledComponent<BoxProps> = styled(Box)(({ theme }) => ({ .......

Comments

3

In case you are the one who created the npm package

Possible problem reason

I had this issue because I created an npm package and didnt export some internal types. typescript couldnt infer the types because it had no access to it.

Solutions

  • 1st solution, Modify the function signature to use exported types only, so it doesnt rely on type infering.
  • 2nd solution, If you want to rely on infer then modify the npm package to export the internal types

Comments

1

Typescript needs a reference for that type so put this at the top of the file that is causing the error:

/// <reference types="@emotion" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.