Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(setupWorker): resolve the TS4094 error #1477

Merged

Conversation

gduliscouet-ubitransport
Copy link
Contributor

Type the return of setupWorker with the interface instead of the class.

I also don't export the class SetupWorkerApi anymore because it was not used in other files in the project, and not exposd in the index.ts (only the interface is exposed)

I tested in local that it fixes #1454

@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 25, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 7be85e0:

Sandbox Source
MSW React Configuration

filipw01
filipw01 previously approved these changes Nov 25, 2022
@@ -19,14 +19,15 @@ import { createFallbackStop } from './stop/createFallbackStop'
import { devUtils } from '../utils/internal/devUtils'
import { SetupApi } from '../SetupApi'
import { mergeRight } from '../utils/internal/mergeRight'
import { SetupWorkerApi as ISetupWorkerApi } from './glossary'
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to export the SetupWorkerApi interface from index.ts just as we do SetupServerApi from node/index.ts. The fact that it wasn't exported before is a mistake.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact it was already the interface that was exported, and not the class !

image


interface Listener {
target: EventTarget
eventType: string
callback: EventListener
}

export class SetupWorkerApi extends SetupApi<WorkerLifecycleEventsMap> {
class SetupWorkerApi extends SetupApi<WorkerLifecycleEventsMap> {
Copy link
Member

Choose a reason for hiding this comment

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

Let's also force this class to implement the SetupWorker interface. Without this enforcement, the only area to catch the incongruence is in the setupWorker return type, which is rather late.

import { SetupWorker } from './glossary'

export class SetupWorkerApi extends SetupApi implements SetupWorker {
  ...
}

export function setupWorker(): SetupWorker { ... }
  • Rename SetupWorkerApi in glossary to SetupWorker
    • Do the same for SetupServerApi
  • Export both SetupWorker interface and SetupWorkerApi class
  • Set SetupWorker as the return type for setupWorker() to fix the type issue.

Would this approach work? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with the implements part, I hesitated to do that.

But as I said above the SetupWorkerApi interface is already exposed, so we should not change its name to avoid a breaking change.

The class was only exposed through setupWorker return type. And it cannot be used due to those TS declaration file limitations.

Tell me what you think and I'll update the PR

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. Let's split this change in two then:

  1. Import SetupWorkerApi type, alias it to SetupWorker, adjust the return type of setupWorker function. Release as a fix.
  2. Open a follow-up that exports the SetupWorkerApi (class) and renames the SetupWorkerApi (interface) in glossary.ts to SetupWorker. Do the same for SetupServer*. Release a breaking change.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems good. I had to check, but in fact exporting the class instead of the interface removes the TS error (I was expecting to still have it)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated this one, I try to open the second one on top of this one

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Great work! Thank you for the discussion and the fix for this issue, @gduliscouet-ubitransport 👏 Welcome to contributors.

@kettanaito kettanaito changed the title fix(types): type setupWorker return with interface fix(setupWorker): resolve the TS4094 error Nov 26, 2022
@kettanaito kettanaito merged commit c268796 into mswjs:main Nov 27, 2022
@kettanaito
Copy link
Member

Released: v0.49.1 🎉

This has been released in v0.49.1!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

@gduliscouet-ubitransport gduliscouet-ubitransport deleted the fix/setup-worker-return-type branch November 28, 2022 09:03
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.

Documentation example doesn't work with TypeScript
3 participants