Skip to content

Idiomatic example with Next.js & TypeScript #1236

Answered by kettanaito
nirtamir2 asked this question in Q&A
Discussion options

You must be logged in to vote

Hey, @nirtamir2.

I believe your question is related to how to include MSW conditionally with import statements. I don't think you can, as modern bundlers extract those statements at build time. I'm not aware if conditional imports are possible, so you will get anything you import that way in your bundle regardless if there's a condition before the import that'd prevent it.

That's the main reason we recommend using require, as dependencies included that way are not statically analyzable. You can annotate the imported objects in TypeScript to keep type safety:

// Note that unconditional type imports are fine.
import type { SetupWorkerApi } from 'msw'
import type { SetupServerApi } from 'msw…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kettanaito
Comment options

@nirtamir2
Comment options

Answer selected by nirtamir2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants