Description
- Rollup Plugin Name: at least
typescript
andnode-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:
And that I'm just trying to import and invoke it.
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()