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

fix: Solved issue with importing @pollyjs projects in TypesScript #500

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sflanker
Copy link

When importing a dependency with multiple module types, TypeScript needs each type of module to have its own type declaration file, and ES module files must have the corresponding extensions (.mjs and .mts).

Description

  • Added conditional export blocks to each relevant package.json file.
  • Updated rollup configuration to use the mjs extension for ES module build output.
  • Added a copy step to rollup configuration to copy the types.d.ts file to the respective dist folders.

Motivation and Context

microsoft/TypeScript#50466

Specifically the issue here is when you use the Node16 or NodeNext moduleResolution mode in a TypeScript project w/ and corresponding module target, tsc errors with error TS2351: This expression is not constructable. Type 'typeof import("node_modules/@pollyjs/adapter-node-http/types")' has no construct signatures.. This is because tsc's type checker is interpreting the module as a CommonJS module, and when a CommonJS module has this type of default export declaration it is necessary to explicitly extract the default property. Here's an example project to reproduce the issue:

package.json

{
  "name": "consumer-package-ts",
  "version": "1.0.0",
  "main": "dist/index.js",
  "type": "module",
  "scripts": {
    "build": "tsc",
    "start": "node dist/index.js"
  },
  "license": "ISC",
  "dependencies": {
    "@pollyjs/adapter-node-http": "6.0.6",
    "@pollyjs/core": "^6.0.6"
  },
  "devDependencies": {
    "@pollyjs/persister": "^6.0.6",
    "@types/node": "^22.5.0",
    "typescript": "^5.5.4"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "lib": ["ES2020"],
    "target": "ES2020",
    "module": "Node16",
    "moduleResolution": "Node16",
    "outDir": "dist"
  },
  "include": ["*.ts"]
}

index.ts

import { Polly } from '@pollyjs/core';
import NodeHttpAdapter from '@pollyjs/adapter-node-http';

const example = new NodeHttpAdapter(new Polly('example'));

While I would argue that TypeScript could do better here, the maintainers are showing no signs of resolving this or even taking the issue seriously.

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have added tests to cover my changes.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • My code follows the code style of this project.
  • My commits and the title of this PR follow the Conventional Commits Specification.
  • I have read the contributing guidelines.

When importing a dependency with multiple module types, TypeScript needs
each type of module to have its own type declaration file, and ES module
files must have to corresponding extensions (.mjs and .mts).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant