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.

Custom _document file example for styled-components throws a TS bug after updating @types/react to ^18.0.0

See original GitHub issue

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.4.0: Fri Mar 18 00:47:26 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T8101
Binaries:
  Node: 17.4.0
  npm: 8.3.1
  Yarn: 1.22.17
  pnpm: N/A
Relevant packages:
  next: 12.1.4
  react: 18.0.0
  react-dom: 18.0.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

I’m not entirely sure if it should be posted here or as an issue in the types/SC repo, so sorry if it shouldn’t be here. After updating @types/react to 18.0.0, I started noticing an error in the _documents.tsx file when using this approach for styled components.. I now get an error which says

Class static side 'typeof MyDocument' incorrectly extends base class static side 'typeof Document'.
  The types returned by 'getInitialProps(...)' are incompatible between these types.
    Type 'Promise<{ styles: Element; html: string; head?: (Element | null)[] | undefined; }>' is not assignable to type 'Promise<DocumentInitialProps>'.
      Type '{ styles: JSX.Element; html: string; head?: (JSX.Element | null)[] | undefined; }' is not assignable to type 'DocumentInitialProps'.
        Type '{ styles: JSX.Element; html: string; head?: (JSX.Element | null)[] | undefined; }' is not assignable to type '{ styles?: ReactFragment | ReactElement<any, string | JSXElementConstructor<any>>[] | undefined; }'.
          Types of property 'styles' are incompatible.
            Type 'Element' is not assignable to type 'ReactFragment | ReactElement<any, string | JSXElementConstructor<any>>[] | undefined'.
              Type 'ReactElement<any, any>' is missing the following properties from type 'ReactElement<any, string | JSXElementConstructor<any>>[]': length, pop, push, concat, and 29 more.ts(2417)

Upon further investigation I discovered that in this part:

      return {
        ...initialProps,
        styles: (
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          </>
        ),
      };

the overwrite of styles seems to be causing trouble. styles are of type styles?: ReactFragment | ReactElement<any, string | JSXElementConstructor<any>>[] | undefined instead of JSX.Element. If we downgrade types back to “^17.0.43”, it doesn’t seem to be a problem anymore.

Expected Behavior

Types should work correctly if the example made by SC is still relevant.

To Reproduce

You can create a new project with create next-app --typescript and add the document from the file I linked above, you should see the error immediately.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:4
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

47reactions
rafae2kcommented, Apr 9, 2022

You can fix it by returning an array as follows:

return {
        ...initialProps,
        styles: [
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          </>,
        ],
      };

I would be glad to contribute an example with types for the use of styled with next.js and Typescript.

Unfortunately, there are no official docs for that.

(Current example with no types)[https://github.com/vercel/next.js/blob/canary/examples/with-styled-components-babel/pages/_document.js]

12reactions
javlundcommented, Apr 11, 2022

You can fix it by returning an array as follows:

return {
        ...initialProps,
        styles: [
          <>
            {initialProps.styles}
            {sheet.getStyleElement()}
          </>,
        ],
      };

I would be glad to contribute an example with types for the use of styled with next.js and Typescript. Unfortunately, there are no official docs for that. (Current example with no types)[https://github.com/vercel/next.js/blob/canary/examples/with-styled-components-babel/pages/_document.js]

This works but It introduced new warning:

Warning: Each child in a list should have a unique “key” prop. See https://reactjs.org/link/warning-keys for more information. at Head (webpack-internal:///./node_modules/next/dist/pages/_document.js:221:1) at html at Html (webpack-internal:///./node_modules/next/dist/pages/_document.js:186:73) at MyDocument (webpack-internal:///./pages/_document.tsx:14:1)

While I haven’t tried it, I’m guessing that could be fixed with an explicit Fragment tag:

import {Fragment} from "react";
// ...
return {
        ...initialProps,
        styles: [
          <Fragment key="1">
            {initialProps.styles}
            {sheet.getStyleElement()}
          </Fragment>,
        ],
      };
Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript and styled-components gives error on import
js' implicitly has an 'any' type. To fix this I renamed the file to *.jsx and the problem went away. I'm just learning...
Read more >
styled component cannot be used as a jsx component
In react app I use 'styled-components' and typescript. TS gives me Error on DrawToolsWrapper: 'Component cannot be used as a JSX component. Its...
Read more >
styled-components | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
React v18.0 – React Blog
In our last post, we shared step-by-step instructions for upgrading your app to React 18. In this post, we'll give an overview of...
Read more >
types/styled-components
This package contains type definitions for styled-components (https://github.com/styled-components/styled-components). Details. Files were ...
Read more >

github_iconTop Related Medium Post

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