zoubingwu / msw-auto-mock

A cli tool to generate random mock data from OpenAPI definition for msw.
246 stars 55 forks source link

Typescript Support for Generated Mocks #48

Open soleo opened 3 months ago

soleo commented 3 months ago

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 commented 1 month ago

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.