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

Documentation example doesn't work with TypeScript #1454

Closed
4 tasks done
filipw01 opened this issue Nov 8, 2022 · 8 comments · Fixed by #1477
Closed
4 tasks done

Documentation example doesn't work with TypeScript #1454

filipw01 opened this issue Nov 8, 2022 · 8 comments · Fixed by #1477
Assignees
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser

Comments

@filipw01
Copy link

filipw01 commented Nov 8, 2022

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 14 or higher

Browsers

No response

Reproduction repository

https://stackblitz.com/edit/node-ifch93?file=index.ts

Reproduction steps

Use documentation example with typescript https://mswjs.io/docs/getting-started/integrate/browser#configure-worker

// src/mocks/browser.js
import { setupWorker } from 'msw'
import { handlers } from './handlers'

// This configures a Service Worker with the given request handlers.
export const worker = setupWorker(...handlers)

Current behavior

Typescript throws multiple
TS4094: Property 'SOMETHING' of exported class expression may not be private or protected.

Expected behavior

I expect typescript to be fine with that

@filipw01 filipw01 added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser labels Nov 8, 2022
@gduliscouet-ubitransport
Copy link
Contributor

I have the same issue (with typescript 4.7.4).

Here is an explaination of the warning from microsoft/TypeScript#30355:

"Exported anonymous classes can't have private or protected members if declaration emit is enabled, because there's no way to represent that in a .d.ts file"

I'm trying this workaround in the meanwhile, taken from here (not sure I'm doing it well, I didn't check everything)

import { setupWorker, type SetupWorkerApi } from 'msw'
import { handlers } from './handlers'

type SetupWorkerApiWithoutPrivate = Omit<SetupWorkerApi, never>

// This configures a Service Worker with the given request handlers.
export const worker = setupWorker(...handlers) as SetupWorkerApiWithoutPrivate

@kettanaito
Copy link
Member

I'm not sure how this is related to MSW as we don't emit anonymous classes. You can see that the SetupServerApi class is named:

This must be something else. The library builds without issues for me on TS 4.8 but I haven't used it in an example project to verify the consumer's side.

@gduliscouet-ubitransport
Copy link
Contributor

You are right, the issue I shared is talking about an "anonymous class". But here you can see the same TS error with a named class: microsoft/TypeScript#17293.

From what I understand, you should be able to do that, but you cannot due to some limitations in the declaration file creation. See this issue regrouping all the issues with .d.ts declaration files: microsoft/TypeScript#35822.

From what I see, the only solution seems to not use private yet in a typescript lib, or apply some workaround on the exported types :/

You can launch the repro of @filipw01 to see the error on consumer side: https://stackblitz.com/edit/node-ifch93?file=index.ts

@andykenward
Copy link

@gduliscouet-ubitransport Adding the type annotation to the worker constant instead of using TypeScript inference allows me to output the definition file without errors.

import { setupWorker, type SetupWorkerApi } from 'msw';

export const worker:SetupWorkerApi = setupWorker(...[]);

@gduliscouet-ubitransport
Copy link
Contributor

I confrim ! Thank you @andykenward for sharing.

In fact I realised that:

  • the SetupWorkerApi that is used to define the return of setupWorker comes from index.d.ts and is a class with private members
  • the SetupWorkerApi that we import comes from SetupApi-0d3126ba.d.ts and it is an interface. That's why it doesn't break (the Omit I did above was useless)

image

image

@gduliscouet-ubitransport
Copy link
Contributor

@kettanaito I opened a PR to reuse the existing interface describing the class SetupWorkerApi. For me it is the only way to have it working on the consumer side (without the above workaround), but maybe you want to check the interface SetupWorkerApi is describing well enough the class SetupWorkerApi (at least TS it is matching according to TS)

@kettanaito
Copy link
Member

Thanks for adding the fix, @gduliscouet-ubitransport!

I left a few comments on how we could export both the type and the class and fix this issue. Hopefully, that direction would also work.

@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.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants