solid icon indicating copy to clipboard operation
solid copied to clipboard

The module can't be synchronously required in CJS modules when using modern `moduleResolution`s

Open Andarist opened this issue 2 years ago • 3 comments

Describe the bug

https://github.com/solidjs/solid/commit/93d44d45f1607871334d4c4ed637a780736445a4 fixed the types visibility issue reported by me here: https://github.com/solidjs/solid/issues/1787 but it didn't quite fix in full how this package can be required at type level

The input code:

// @moduleResolution: node16
// @module: commonjs
import {} from 'solid-js' // types: 1.7.7

results in

The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("solid-js")' call instead.
  To convert this file to an ECMAScript module, create a local package.json file with `{ "type": "module" }`.(1479)

You can also verify this here: https://arethetypeswrong.github.io/?p=solid-js%401.7.7

Your Example Website or App

https://www.typescriptlang.org/play?moduleResolution=3&target=99&module=1&ts=5.1.3#code/PTAEAEFsHsBMFcA2BTASsgztR8AuBLaAOwC5Qi5kBGANgCgQIYEUyBjaSGIgKwzvyQADtABOuUAG8AvqABmozqADkWRPlgBaPstCNcATyGYyVAHQB2S0A

Steps to Reproduce the Bug or Issue

Configure the tsconfig.json using the mentioned options

Expected behavior

Since at runtime I can require('solid-js'), I should be also able to load this synchronously at type-level

Screenshots or Videos

No response

Platform

N/A

Additional context

TS, rightfully, assumes that this module can't be required in CJS and it might suggest using a dynamic import. Since your package.json#type is set to module and you are using .d.ts files for your type declarations, TS can only assume that those correspond to .js files and that they represent non-requireable modules.

The only true solution to this problem is to ship your types twice. With your current setup, you should be shipping .d.ts and .d.cts declarations.

Andarist avatar Jun 27 '23 07:06 Andarist

This lint may be helpful: https://publint.dev/[email protected]

awhitford avatar Sep 10 '23 03:09 awhitford

Seems that this is also an issue when using a package which itself imports Solid types. Solid's types won't get through the package meaning type errors won't be reported in user code when running tsc, which means bugs silently make their way to production.

May be helpful for tackling this issue: https://arethetypeswrong.github.io/?p=solid-js%401.8.3

aryzing avatar Oct 21 '23 18:10 aryzing