hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client
https://heyapi.vercel.app
MIT License
635 stars 44 forks source link

`Spread types may only be created from object types` issue for additionalHeaders in generated `requests.ts` #706

Closed fnep closed 1 week ago

fnep commented 1 week ago

Description

With the update from version 0.46.3 to 0.47.2, typescript is throwing an error in the generated requests.ts.

I'm not really sure what to do with it. 😕

webpack 5.91.0 compiled successfully in 2809 ms
ERROR in ./src/backend/v1/core/request.ts:127:9
TS2698: Spread types may only be created from object types.
    125 |     const headers = Object.entries({
    126 |         Accept: "application/json",
  > 127 |         ...additionalHeaders,
        |         ^^^^^^^^^^^^^^^^^^^^
    128 |         ...options.headers,
    129 |     })
    130 |         .filter(([, value]) => value !== undefined && value !== null)

OpenAPI specification (optional)

No response

Configuration

export default defineConfig({
    input: "openapi.json",
    output: "src/backend/v1",
    services: {
        asClass: true,
    },
});

System information (optional)

No response

mrlubos commented 1 week ago

Does this affect all operations?

mrlubos commented 1 week ago

Would you be able to create a minimal reproducible example on StackBlitz?

fnep commented 1 week ago

Does this affect all operations?

Not sure if I get the question right. It is an error during compile time. Actually, I assume in practice the code will work.

create a minimal reproducible example on StackBlitz

Im new to StackBlitz but i gave it a try. I hope it works for you.

-> https://stackblitz.com/edit/webpack-webpack-js-org-maeyov?file=package.json

I used

  1. npm exec --yes @hey-api/openapi-ts@0.47.2
  2. webpack

to get the error.

harmen-xb commented 1 week ago

I had the same issue. I think it's related to the typescript version you use. With typescript 4 I get the same error. With typescript ^5.00 it works for me.

fnep commented 1 week ago

I'm on typescript 5.3.3 in my project, and the StackBlitz example above is on 5.5.2.

mrlubos commented 1 week ago

@fnep this will be fixed in the next release, but I suspect it won't solve all your issues. I tried with your example and after fixing this issue, I was getting a weird Webpack error failing to build the services.gen.ts file if I understand it correctly. I believe this might be something to do with the Webpack config itself, but open a new issue if you find out more and think something needs to be fixed in this package.

fnep commented 4 days ago

Just updated to 0.48.0 and for me, this solved the issue entirely. Thank you! 🙂