Skip to content

[BUG][typescript-angular] Enum generation with 'as const' breaks Angular FormControl generic typing #21440

@majcen-dev

Description

@majcen-dev

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

In OpenAPI Generator 7.13.0, the typescript-angular generator changed enum generation to use the as const pattern:

export const MyEnum = {
  ValueA: 'ValueA',
  ValueB: 'ValueB'
} as const;
export type MyEnum = typeof MyEnum[keyof typeof MyEnum];

While this improves TypeScript typing, it breaks some Angular use cases involving generics, such as:

type MyFormGroup = {
  myControl: FormControl<MyEnum>;
}

export class MyClass {
  myformGroup: FormGroup<MyFormGroup> = new FormGroup({
      myControl: new FormControl(MyEnum.ValueA),
  });
}

And produces the following error:

TS2322: Type 'FormGroup<{ myControl: FormControl<"ValueA">; }>' is not assignable to type 'FormGroup<MyFormGroup>'.
  Type 'FormControl<MyEnum>' is not assignable to type 'FormControl<"ValueA">'.
    Type 'MyEnum' is not assignable to type '"ValueA"'.
      Type '"ValueB"' is not assignable to type '"ValueA"'.

EDIT: I have updated the example above with a more relevant one. Before I tried to keep it small, but that specific snippet didn't actually trigger the error.

because MyEnum.ValueA is inferred as a string literal instead of the union type, causing TypeScript errors or forcing unsafe casts. This could be seen as a regression from the previous enum output style in certain cases.

openapi-generator version

v7.13.0

Generation Details
{
  "openAPIUrl": "...../swagger.json",
  "options": {"ngVersion": "15.1.2"},
}
Steps to reproduce

The above example will trigger a type error.

Related issues/PRs

This change was introduced in #20958, which updated enum generation to use the as const pattern for better typing.

Suggest a fix

Consider adding an optional config flag (e.g., useOldEnumStyle=true) to allow reverting to the previous enum format for compatibility.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions