Skip to content

fix: importing msw/experimental throws runtime error#2702

Open
christoph-fricke wants to merge 1 commit intomswjs:mainfrom
christoph-fricke:fix/types-imported-as-value
Open

fix: importing msw/experimental throws runtime error#2702
christoph-fricke wants to merge 1 commit intomswjs:mainfrom
christoph-fricke:fix/types-imported-as-value

Conversation

@christoph-fricke
Copy link
Copy Markdown
Contributor

@christoph-fricke christoph-fricke commented Apr 10, 2026

The new network-source API is not usable as soon as a project tries to import from msw/experimental. It crashes with a runtime error:

SyntaxError: The requested module './define-network.mjs' does not provide an export named 'DefineNetworkOptions'

This PR fixes the problem by using a type export in src/core/experimental/index.ts to ensure DefineNetworkOptions is stripped from the JS output.


I initially tried to prevent such problems in the future by enabling verbatimModule syntax in tsconfig.base.json and fixing multiple TS errors reported by pnpm exec tsc --noEmit -p src/tsconfig.src.json, but it caused strange problems in CI.

Besides this, there appear to be multiple TS errors in the project. Some picked in the editor, some only reported by checking with one of the various TS config files such as pnpm exec tsc --noEmit -p ./tsconfig.test.unit.json. Some source code errors I noticed:

  • /src/browser/sources/fallback-http-source.ts
  • /src/browser/utils/deserializeRequest.ts
  • /src/browser/utils/pruneGetRequestBody.ts

It does not look like the CI finds and reports the errors. Are you aware of these existing TypeScript problems?

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 10, 2026

📝 Walkthrough

Walkthrough

This change converts DefineNetworkOptions from a runtime export and import to type-only exports and imports across two core files, while preserving the defineNetwork function export. The modification reduces JavaScript bundle output by removing the type from runtime values.

Changes

Cohort / File(s) Summary
Type-only Export Conversion
src/core/experimental/index.ts, src/native/index.ts
Converted DefineNetworkOptions from runtime export/import to type-only syntax, eliminating it from JavaScript output while maintaining TypeScript type information.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

A rabbit's optimization hop! 🐰
DefineNetworkOptions takes flight so pure,
From runtime to types, the bundle's secure,
TypeScript magic, clean and bright,
Type-only exports shining light! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Title check ✅ Passed The title directly addresses the main issue: fixing a runtime error caused by importing from msw/experimental due to type-only symbols being exported as values.
Description check ✅ Passed The PR description clearly explains the runtime error, the fix applied (type-only export), and provides context about related TypeScript issues.

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

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

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

@christoph-fricke christoph-fricke force-pushed the fix/types-imported-as-value branch 2 times, most recently from aee80f6 to 0c34cc6 Compare April 10, 2026 12:41
@@ -1,12 +1,12 @@
import { pruneGetRequestBody } from './pruneGetRequestBody'
import type { ServiceWorkerIncomingRequest } from '../glossary'
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type does not exist in glossary...

@christoph-fricke christoph-fricke force-pushed the fix/types-imported-as-value branch from 0c34cc6 to fb8cda5 Compare April 10, 2026 14:17
@christoph-fricke christoph-fricke changed the title fix: mark all type imports and exports as type-only fix: importing msw/experimental throws runtime error Apr 10, 2026
@@ -1,4 +1,5 @@
export { defineNetwork, DefineNetworkOptions } from './define-network'
export { defineNetwork } from './define-network'
export type { DefineNetworkOptions } from './define-network'
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason it has to be this separate export type {....}. export {..., type ...} appears to not strip the interface from the output /lib/core/experimental/index.mjs file (unless I was looking at some cached file...).

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