Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[email protected] gives "Type '() => any' is not a constructor function type" error in _Document.tsx #32110

Closed
JamesSingleton opened this issue Dec 4, 2021 · 10 comments · Fixed by #32117
Labels
bug Issue was opened via the bug report template.

Comments

@JamesSingleton
Copy link

JamesSingleton commented Dec 4, 2021

What version of Next.js are you using?

12.0.5

What version of Node.js are you using?

14.18.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

After upgrading to both [email protected] and [email protected], my _Document.tsx now throws an eslint error Type '() => any' is not a constructor function type. I have typescript@^4.5.2 installed.

_Doucment.tsx

import Document, { Head, Html, Main, NextScript } from 'next/document'

class MyDocument extends Document {
  render() {
    return (
      <Html lang="en-US" className="h-full bg-gray-100">
        <Head />
        <body className="loading h-full">
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument

.eslintrc.json

{
  "extends": ["next/core-web-vitals", "prettier"],
  "rules": {
    "react/no-unescaped-entities": "off"
  }
}

Expected Behavior

This was not breaking on 12.0.4 and should not break on 12.0.5

To Reproduce

Create a Next.js project with the example code I provided above.

@JamesSingleton JamesSingleton added the bug Issue was opened via the bug report template. label Dec 4, 2021
@indiejoseph
Copy link

indiejoseph commented Dec 4, 2021

+1, just upgrade to 12.0.5 and got this problem.

@emt-mocha
Copy link

emt-mocha commented Dec 4, 2021

upgraded to 12.0.5 and facing the same issue during build time. I am using:
Typescript = 4.4.4
Eslint = 7.32.0
eslint-config-next = 12.0.5
nodejs = v16.13.0

After downgrade to [email protected] the issue is no longer exist.

@TimNZ
Copy link

TimNZ commented Dec 4, 2021

I respect and appreciate the awesome work of the Next js team,
so I am perplexed how this got through testing for release.

For now replace

import Document from 'next/document'

with

import Document from 'next/dist/pages/_document'

document.d.ts has been broken

12.0.5

import Document from './dist/shared/lib/runtime-config'
export * from './dist/pages/_document'
export default Document

12.0.4

export * from './dist/pages/_document'
export { default } from './dist/pages/_document'

@raulfdm
Copy link
Contributor

raulfdm commented Dec 4, 2021

It seems the usage is different now... or at least that's how the types are defined:
https://github.com/vercel/next.js/pull/31560/files#diff-82360298eb44f75969189468a71ab683227c0848c9b4fecbeae1321a7c1680b3R46

The error go away if I do:

export default class MyDocument extends Document() {
 // ...rest
}

But if that's the case, it seems a breaking change. Who customize _document will need to change to that.


Update

I run a build with that syntax and got an error:

TypeError: Class constructor Document cannot be invoked without 'new'

So I think the interface is incorrect


Workaround

Quick and dirty:

import Document, { Head, Html, Main, NextScript } from 'next/document';

const CustomDocument = Document as any

export default class MyDocument extends CustomDocument {
  // ... rest
}

@TimNZ
Copy link

TimNZ commented Dec 4, 2021

@raulfdm
#32110 (comment)

@timneutkens
Copy link
Member

Hey! Thanks for the report and investigation. I've opened a PR here: #32117. The reason this got through to a release is that there was no custom _document.tsx / _app.tsx in the TypeScript test suites, I've added those in #32117.

@kodiakhq kodiakhq bot closed this as completed in #32117 Dec 4, 2021
kodiakhq bot pushed a commit that referenced this issue Dec 4, 2021
There was a mistake in #32077 which imported a different module than _document in the _document .d.ts file. I found that we didn't have a test for a custom _document.tsx and _app.tsx with TypeScript, so I've added one that fails on canary and passes with this fix to ensure this does not happen in the future.

Fixes #32110




## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
@timneutkens
Copy link
Member

Published 12.0.6: https://github.com/vercel/next.js/releases/tag/v12.0.6

It'll take a bit for it to be available on npm 🙏

@captain-woof

This comment has been minimized.

@captain-woof
Copy link

Published 12.0.6: https://github.com/vercel/next.js/releases/tag/v12.0.6

It'll take a bit for it to be available on npm pray

Awesome!

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
natew pushed a commit to natew/next.js that referenced this issue Feb 16, 2022
There was a mistake in vercel#32077 which imported a different module than _document in the _document .d.ts file. I found that we didn't have a test for a custom _document.tsx and _app.tsx with TypeScript, so I've added one that fails on canary and passes with this fix to ensure this does not happen in the future.

Fixes vercel#32110




## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants