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

Typescript Support for Generated Mocks #48

Open
soleo opened this issue Mar 15, 2024 · 3 comments
Open

Typescript Support for Generated Mocks #48

soleo opened this issue Mar 15, 2024 · 3 comments

Comments

@soleo
Copy link

soleo commented Mar 15, 2024

For our organization use case, we usually generate the mocks by running

npx msw-auto-mock ./swagger.json -o ./src/generated/mock.ts --base-url ${BASE_URL} --codes 200,201,204

If we don't ingnore eslint for this file, we always end up with the following complaint.

ERROR in ./src/generated/mock.ts:123:31
TS7005: Variable 'resultArray' implicitly has an 'any[]' type.
    121 | 		const resultArray = [];
    122 |
  > 123 | 		return HttpResponse.json(...resultArray[next() % resultArray.length]);
        | 		                            ^^^^^^^^^^^
    124 | 	}),

I'd propose another new option('-s, --typescript', 'Generate TypeScript code.') for generating more strict typed mocks. If it is a good idea, I'd like to help get a PR done as well.

@renet
Copy link

renet commented May 27, 2024

I'm not sure if I agree here. Don't get me wrong, I love TypeScript. But generated files should probably rather get ignored instead of linted (or formatted). Every user has a different lint configuration and there will probably always be a rule that is too strict for the generated file. I think it's not reasonable, or even possible, to write the output file in a way that every possible linting rule is happy. Maybe consider ignoring the generated files in your eslint config, instead.

@MrMarble
Copy link

MrMarble commented Sep 17, 2024

The types of the internal structure can be any but I think the exported members should have types, just to solve the complaints when using them without having to ignore errors.
For example in our organization we have a ver simple handlers.d.ts

declare module '@/mock/handlers' {
  import { rest } from 'msw';

  export const handlers: rest.RequestHandler[];
}

This way we don't get the error about type definitions when importing the handlers

@nikosanctioned
Copy link

please do it

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

No branches or pull requests

4 participants