Skip to content

Can't correctly import plugins into a TypeScript module #1541

Closed
@lazarljubenovic

Description

@lazarljubenovic
  • Rollup Plugin Name: at least typescript and node-resolve, probably all
  • Rollup Plugin Version: typescript at 11.1.2, node-resolve at 15.1.0 (currently latest)
  • Rollup Version: 3.25.1 (currently latest)
  • Operating System (or Browser): Ubuntu
  • Node Version: 16.14.1
  • Link to reproduction (⚠️ read below): https://github.com/lazarljubenovic/issue-repro-rollup-plugin

The repo is really minimal. The important part is that it's a module:

https://github.com/lazarljubenovic/issue-repro-rollup-plugin/blob/097e003ae506342974740a58788e65f7a951ad40/package.json#L5

And that I'm just trying to import and invoke it.

https://github.com/lazarljubenovic/issue-repro-rollup-plugin/blob/097e003ae506342974740a58788e65f7a951ad40/index.ts#L1-2

Expected Behavior

It compiles.

Actual Behavior

It gives the following type error.

index.ts:2:1 - error TS2349: This expression is not callable.
  Type 'typeof import("/home/lazar/dummy-01/node_modules/@rollup/plugin-typescript/types/index")' has no call signatures.

The generated codes works, though -- the function call is successfully performed and returns the correct value.

Additional Information

The issue is happening because the type of my import is treated as if it was written in CommonJS. In other words, even thought the runtime will perform import typescript from '@rollup/plugin-typescript', types are resolved as if I've written import typescript = require('@rollup/plugin-typescript'). This explains the error, where TS believes that typescript is the whole module. In fact, if I change my code to typescript.default(), it compiles but fails at runtime because the default export is a function without a property default.

The root cause is simply the way TS works at the moment. It's a surprising behvaior, but it's documented:

It’s important to note that the CommonJS entrypoint and the ES module entrypoint each needs its own declaration file, even if the contents are the same between them. Every declaration file is interpreted either as a CommonJS module or as an ES module, based on its file extension and the "type" field of the package.json, and this detected module kind must match the module kind that Node will detect for the corresponding JavaScript file for type checking to be correct. Attempting to use a single .d.ts file to type both an ES module entrypoint and a CommonJS entrypoint will cause TypeScript to think only one of those entrypoints exists, causing compiler errors for users of the package.

Since the extension is .d.ts, and the rollup plugin package is not a module, TypeScript determines that .d.ts is always a CJS module. The exports.types setting is correctly used, but what it points to is interpreted as CJS.

Therefore, the package must be distributed with two distinct declaration files. For example, after making the following changes in my node_modules folder, everything works correctly.

  + types
- +-- index.d.ts
+ +-- index.d.mts
+ +-- index.d.cts
  "exports": {
-   "types": "./types/index.d.ts",
-   "import": "./dist/es/index.js",
+   "import": {
+     "types": "./types/index.d.mts",
+     "default": "./dist/es/index.js"
+   },
+   "require": {
+     "types": "./types/index.d.cts",
+     "default": "./dist/cjs/index.js"
+   },
    "default": "./dist/cjs/index.js"
  },

The contents of index.d.mts and index.d.cts are identical to the original index.d.ts.

This works with both "type": "module" and "type": "commonjs".

// with "type": "module", this works now
import typescript from '@rollup/plugin-typescript'
typescript()
// with "type": "commonjs", this also works
import typescript = require('@rollup/plugin-typescript')
typescript.default()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions