types: `export default` is wrong under moduleResolution NodeNext

Open Beginner friendly
#1,536 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, typescript

Research direction

Start with types/index.d.ts and inspect its export declaration alongside package.json's types, import, and require entries. Verify the declaration with a minimal TypeScript consumer using moduleResolution NodeNext, checking both new PptxGenJS() and PptxGenJS.TableCell. Done means those examples typecheck while the package continues to describe its published builds.

Written by the indexing model from the issue text.

Description

Category

  • Bug
  • Enhancement

Version

  • pptxgenjs 4.0.1 (also current master types/index.d.ts)
  • TypeScript module / moduleResolution: NodeNext
  • Consumer package is native ESM ("type": "module")

Observed behavior

types/index.d.ts ships:

export as namespace PptxGenJS
export default PptxGenJS
declare class PptxGenJS { … }
declare namespace PptxGenJS { … }

The npm package is not "type": "module". package.json exposes both builds:

"main": "dist/pptxgen.cjs.js",
"module": "dist/pptxgen.es.js",
"exports": {
  "types": "./types/index.d.ts",
  "import": "./dist/pptxgen.es.js",
  "require": "./dist/pptxgen.cjs.js"
}

Under moduleResolution: NodeNext, TypeScript reads that single .d.ts as a CommonJS module. In that mode export default PptxGenJS means module.exports.default, which neither published build has (the CJS build is module.exports = PptxGenJS).

That makes both of these fail typechecking:

import PptxGenJS from 'pptxgenjs';
const pptx = new PptxGenJS(); // not constructable
const cell: PptxGenJS.TableCell = …; // namespace merge is also wrong

This is a known class of CJS + export default mismatch (see SO: pptxgenjs is not constructable).

Expected behavior

The declarations should describe the actual CJS export shape so new PptxGenJS() and PptxGenJS.TableCell typecheck under NodeNext, while still supporting the ESM build.

Changing the types line to export = PptxGenJS matches module.exports = PptxGenJS and keeps the class/namespace merge. That is the workaround we are using via a package patch today.

Suggested fix

In types/index.d.ts:

-export default PptxGenJS
+export = PptxGenJS

Happy to open a PR if that direction looks right. Once this ships we can drop the downstream patch.

Dominant language
TypeScript
Stars
6.2k
Forks
953
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from gitbrent/PptxGenJS

All issues in gitbrent/PptxGenJS

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.