Skip to content

LegendPosition enum break template type checks #1955

@kschaefe

Description

@kschaefe

Describe the bug
Angular's fullTemplateTypeCheck wants the passed type of LegendPosition to be a value from the enum. Passing in the string that represents that value causes the compiler to reject the string. You have the

To Reproduce
Manually set the LegendPosition to a string, such as right or below and compile with fullTemplateTypeCheck. You'll get an error like

Building Angular Package

------------------------------------------------------------------------------
Building entry point 'livelab'
------------------------------------------------------------------------------
✖ Compiling with Angular sources in Ivy partial compilation mode.
projects/livelab/src/lib/student-details/livelab-student-details.component.html:127:49 - error TS2322: Type '"below"' is not assignable to type 'LegendPosition'.

127             [legend]="true" [legendTitle]="''" [legendPosition]="'below'" [yScaleMax]="yScaleMax" [yScaleMin]="0"
                                                    ~~~~~~~~~~~~~~

  projects/livelab/src/lib/student-details/livelab-student-details.component.ts:33:16
    33   templateUrl: "./livelab-student-details.component.html",
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Error occurs in the template of component LivelabStudentDetailsComponent.

Expected behavior
Unfortunately, this is the expected behavior because enums are bad. You want, per your documentation, that value to be a string, but it's not. The source code should be:

export type LegendPosition = 'right' | 'below';

ngx-charts version
Present on any version that uses LegendPosition enum. Experienced on 20.5.0.

Additional context
You can work around this issue using Angular's $any(), but then why enable strict type checking?

Ideally, you'd similarly convert any other enums to a union of string literals, which is almost always better anyway. I, however, only attempted to use this enum.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions