openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

Compile error with default value for string enum

Open dieseldjango opened this issue 2 years ago • 3 comments

Describe the bug I have the following string enum schema for a query parameter with a default value:

   OrderByDirection:
      description: The sort order for query results.
      type: string
      enum: [asc, desc]
      default: desc

The generated client doesn't compile because you can't assign a string literal to an enum:

TSError: ⨯ Unable to compile TypeScript:
../shared/src/ApiClient/services/V1Service.ts:24:9 - error TS2322: Type '"desc"' is not assignable to type 'OrderByDirection'.

24         orderByDirection: OrderByDirection = 'desc',
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

dieseldjango avatar Feb 23 '23 17:02 dieseldjango

I'm also running into this issue. The simplest fix from the codegen side might be to wrap the enum in a template literal, e.g.

orderByDirection: `${OrderByDirection}` = 'desc',

If this is feasible I'm happy to do a PR. In the meantime, I've been using the --useUnionTypes to not generate enums at all.

nerdyman avatar Jan 23 '25 14:01 nerdyman

Hi @nerdyman, this package isn't maintained at the moment so a pull request is unlikely to be merged I'm afraid

mrlubos avatar Jan 23 '25 14:01 mrlubos

No worries, thanks for the quick reply @mrlubos

nerdyman avatar Jan 23 '25 14:01 nerdyman