Skip to content

feat(typescript): add isolatedModules option for export type / import type support#2543

Open
chengyixu wants to merge 1 commit into
asyncapi:masterfrom
chengyixu:feat/typescript-isolated-modules-export-type
Open

feat(typescript): add isolatedModules option for export type / import type support#2543
chengyixu wants to merge 1 commit into
asyncapi:masterfrom
chengyixu:feat/typescript-isolated-modules-export-type

Conversation

@chengyixu

Copy link
Copy Markdown
Contributor

Description

Adds a new isolatedModules option to TypeScriptOptions (default: false, opt-in). When set to true with ESM named exports, the generator emits export type { } for type-only models (interfaces, type aliases) and import type { } for cross-model dependencies.

This fixes TS1205: Re-exporting a type when 'isolatedModules' is enabled requires using 'export type'. errors that occur in Next.js, SWC, and esbuild projects.

Before (broken with isolatedModules: true):

interface ErrorPayload {
  type: 'error';
  message: string;
}
export { ErrorPayload }; // TS1205 error

After (with isolatedModules: true):

interface ErrorPayload {
  type: 'error';
  message: string;
}
export type { ErrorPayload }; // isolatedModules compatible

Key design decisions:

  • Enums are always exported as export { } (never export type) because they compile to runtime JavaScript values.
  • The option is opt-in (isolatedModules: false by default) so existing users are unaffected.
  • Cross-model dependencies also use import type { } when the flag is enabled and the imported model is type-only.

Usage:

const generator = new TypeScriptGenerator({
  modelType: 'interface',
  moduleSystem: 'ESM',
  isolatedModules: true, // new option
});
const models = await generator.generateCompleteModels(doc, { exportType: 'named' });

Related Issue

Closes #2429

Checklist

  • The code follows the project's coding standards and is properly linted (npm run lint).
  • Tests have been added or updated to cover the changes.
  • Documentation has been updated to reflect the changes.
  • All tests pass successfully locally.(npm run test).

Additional Notes

  • 9 new unit tests in TypeScriptDependencyManager.spec.ts covering all export/import type combinations.
  • 2 new integration tests + snapshots in TypeScriptGenerator.spec.ts validating end-to-end output.
  • All 110 existing TypeScript generator tests continue to pass.

@chengyixu chengyixu requested a review from Samridhi-98 as a code owner May 4, 2026 12:27
@netlify

netlify Bot commented May 4, 2026

Copy link
Copy Markdown

Deploy Preview for modelina canceled.

Name Link
🔨 Latest commit 796c905
🔍 Latest deploy log https://app.netlify.com/projects/modelina/deploys/69feceb241cd3d0008950024

@chengyixu

Copy link
Copy Markdown
Contributor Author

Hi @Samridhi-98 — just created this PR adding isolatedModules support for TypeScript ESM exports. This fixes TS1205 errors in Next.js/SWC/esbuild projects. All CI checks are passing (SonarCloud). Would love a review when you have a chance. Happy to adjust anything!

@chengyixu

Copy link
Copy Markdown
Contributor Author

Hi @Samridhi-98 — you've been requested to review this PR. It adds an isolatedModules TypeScript option that emits export type { } for type-only models, fixing TS1205 errors in Next.js/SWC/esbuild projects. All CI checks are passing (SonarCloud, lint). Would appreciate your review when you get a chance! Happy to make any adjustments needed.

@chengyixu

Copy link
Copy Markdown
Contributor Author

Hi @jonaslagoni @magicmatatjahu — this PR adds an isolatedModules option to the TypeScript generator, emitting export type { } for type-only models to fix TS1205 errors in Next.js/SWC/esbuild projects. SonarCloud and Netlify checks are passing. The fork workflow CI (Lint PR title, PR testing) needs maintainer approval to run. Would appreciate a review or CI approval when you get a chance. Happy to adjust anything!

@chengyixu

Copy link
Copy Markdown
Contributor Author

Hi! This PR is CI green and ready for merge when you have a moment. Thanks!

When TypeScript projects use isolatedModules: true (required by Next.js,
SWC, and esbuild), type-only re-exports must use 'export type { }' syntax
instead of 'export { }'. Without this, users get TS1205 errors:

  error TS1205: Re-exporting a type when 'isolatedModules' is enabled
  requires using 'export type'.

This commit adds a new TypeScriptOptions.isolatedModules boolean (default
false, opt-in). When enabled with ESM named exports:

- renderExport() emits 'export type { ModelName }' for interfaces and
  type aliases (any non-enum model), keeping 'export { }' for enums
  since they are runtime values.
- renderCompleteModelDependencies() emits 'import type { ModelName }'
  for cross-model dependencies that are type-only.

Enums are intentionally excluded from export type because they compile
to runtime JavaScript objects.

Fixes: asyncapi#2429
@chengyixu chengyixu force-pushed the feat/typescript-isolated-modules-export-type branch from 29fb282 to 796c905 Compare May 9, 2026 06:05
@sonarqubecloud

sonarqubecloud Bot commented May 9, 2026

Copy link
Copy Markdown

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.

TypeScript: Add support for export type syntax for interfaces (isolatedModules compatibility)

1 participant