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

TypeScript types fail with ESM (type: module) + TSConfig module: node16 #46676

Closed
1 task done
karlhorky opened this issue Mar 2, 2023 · 5 comments
Closed
1 task done
Labels
bug Issue was opened via the bug report template. TypeScript Related to types with Next.js.

Comments

@karlhorky
Copy link
Contributor

karlhorky commented Mar 2, 2023

Verify canary release

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

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #22 SMP Tue Jan 10 18:39:00 UTC 2023
    Binaries:
      Node: 16.17.0
      npm: 8.15.0
      Yarn: 1.22.19
      pnpm: 7.1.0
    Relevant packages:
      next: 13.2.4-canary.0
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

TypeScript support

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/floral-thunder-44jpww?file=%2Fpages%2Findex.tsx

To Reproduce

  1. Open reproduction above
  2. Observe TypeScript error on dynamic() function
    This expression is not callable.
    Type 'typeof import("/project/sandbox/node_modules/next/dynamic")' has no call signatures.
    
  3. Observe TypeScript error on Image component
    JSX element type 'Image' does not have any construct or call signatures.typescript(2604)
    

Screenshot 2023-03-02 at 12 18 18

Screenshot 2023-03-02 at 12 16 58

This occurs with all import paths with slashes into next eg:

  • next/dynamic
  • next/image
  • next/link
  • next/navigation
  • next/script

Describe the Bug

The TypeScript types for the imports at the paths mentioned above fail when using:

  1. ESM ("type": "module" in package.json)
  2. TSConfig "module": "node16", which was seemingly implemented by @loettz in Add support for tsconfig 'nodenext' | 'node16' #44177

The code however still runs, so it seems to be a problem with the Next.js types and ESM <> CommonJS interop.

I'm guessing that since Next.js is CommonJS, the types of these exports on all *.d.ts files are actually wrong:

import Link from './dist/client/link'
export * from './dist/client/link'
export default Link

I guess the correct type would actually use the unusual TypeScript syntax export =, like my PR over here:

https://github.com/DefinitelyTyped/DefinitelyTyped/pull/64137/files

cc @andrewbranch @fluggo @cseas @lfades

Workaround

A workaround is to type the import as the .default property, as @kachkaev shows in this discussion comment:

-import x from "x";
+import _x from "x";
+const x = _x as unknown as typeof _x.default;

Expected Behavior

The import should work out of the box and no TypeScript errors should appear.

See also

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@karlhorky karlhorky added the bug Issue was opened via the bug report template. label Mar 2, 2023
@github-actions github-actions bot added the TypeScript Related to types with Next.js. label Mar 2, 2023
@karlhorky
Copy link
Contributor Author

I'm guessing that since Next.js is CommonJS, the types of these exports on all *.d.ts files are actually wrong:

import Link from './dist/client/link'
export * from './dist/client/link'
export default Link

I guess the correct type would actually use the unusual TypeScript syntax export =, like my PR over here:

https://github.com/DefinitelyTyped/DefinitelyTyped/pull/64137/files

Also added this to the issue description above.

@karlhorky
Copy link
Contributor Author

karlhorky commented Mar 2, 2023

Indeed, adding this change here fixed the problem:

 import Link from './dist/client/link' 
 export * from './dist/client/link' 
-export default Link 
+export = Link 

@lfades @timneutkens would you accept a PR for this for all *.d.ts files?

If you're skeptical of this syntax (I was too at first), there are a bunch of PRs (including this one linked in the description) that I have done recently where @andrewbranch has confirmed that this is correct and will not cause problems for other TypeScript configurations.

@cseas
Copy link
Contributor

cseas commented Mar 2, 2023

I'm facing this issue as well here.

Screenshot 2023-03-02 at 4 59 15 PM

To work around this temporarily, I'm building my project with ignoreBuildErrors option.

Are The Types Wrong shows that the next package is not bundled properly to be consumed by node16 moduleResolution in an ESM project.

Screenshot 2023-03-02 at 5 02 27 PM

Related bugs:

@balazsorban44
Copy link
Member

Closing as duplicate of #46078

@github-actions
Copy link
Contributor

github-actions bot commented Apr 6, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 6, 2023
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. TypeScript Related to types with Next.js.
Projects
None yet
Development

No branches or pull requests

3 participants