Skip to content

fix: add missing type for default class exports#542

Draft
ilyaliao wants to merge 1 commit into
unjs:mainfrom
ilyaliao:fix/default-class-dts
Draft

fix: add missing type for default class exports#542
ilyaliao wants to merge 1 commit into
unjs:mainfrom
ilyaliao:fix/default-class-dts

Conversation

@ilyaliao

@ilyaliao ilyaliao commented May 25, 2026

Copy link
Copy Markdown
Contributor

This PR adds the missing type for export default class X so the generated d.ts surfaces default as X in the type re-export block. Without it, X is only registered as a value and cannot be used as a type.

Source:

// classes/Foo.ts
export default class Foo {}
export class Bar {}

Before:

declare global {
  const Bar: typeof import('./classes/Foo').Bar
  const Foo: typeof import('./classes/Foo').default
}
// for type re-export
declare global {
  // @ts-ignore
  export type { Bar, Foo } from './classes/Foo'
  import('./classes/Foo')
}

After:

declare global {
  const Bar: typeof import('./classes/Foo').Bar
  const Foo: typeof import('./classes/Foo').default
}
// for type re-export
declare global {
  // @ts-ignore
  export type { default as Foo, Bar } from './classes/Foo'
  import('./classes/Foo')
}

waiting on unjs/mlly#354 to be merged

Summary by CodeRabbit

  • Improvements
    • Enhanced export scanning so export default class ... is recognized and correctly reflected in generated type declarations.
  • Tests
    • Updated inline snapshots to cover the new default class export handling and ensure consistent declare global output.

@coderabbitai

coderabbitai Bot commented May 25, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds detection for default-exported class declarations in scan-dirs.ts. A new regex constant identifies export default class patterns, and the export scanning logic conditionally emits a type-only import entry when a default export matches this pattern. A playground fixture and test snapshots verify the behavior.

Changes

Default-exported class detection

Layer / File(s) Summary
Test fixture for default-exported class
playground/composables/DefaultClass.ts
A new module exports an empty DefaultClass as a default export to serve as test subject.
Default-exported class detection and type-only import emission
src/node/scan-dirs.ts
Introduces regex RE_DEFAULT_CLASS_EXPORT to recognize export default class syntax and extends scanExports to emit a type-only import entry when the default export is a class.
Test snapshot updates for class export detection
test/scan-dirs.test.ts, test/dts.test.ts
Snapshots verify detection behavior: scanDirExports expects both a value export and a type-only class export entry for DefaultClass, and dts snapshots include DefaultClass in global constants and type re-export logic.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A class exported by default takes flight,
But type-checking needs it held tight—
The regex catches what must be known,
So imports of types stay cleanly shown. 🐰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: add missing type for default class exports' accurately summarizes the main change: adding proper type support for default class exports in generated d.ts files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented May 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 94.28%. Comparing base (a4a5e0a) to head (ff39068).
⚠️ Report is 172 commits behind head on main.

Files with missing lines Patch % Lines
src/node/scan-dirs.ts 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #542      +/-   ##
==========================================
- Coverage   98.73%   94.28%   -4.45%     
==========================================
  Files          14       15       +1     
  Lines        1817     1033     -784     
  Branches      374      341      -33     
==========================================
- Hits         1794      974     -820     
- Misses         23       54      +31     
- Partials        0        5       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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