Skip to content

Add CheckExhaustive directive for NgSwitch #49089

@baron816

Description

@baron816

Which @angular/* package(s) are relevant/related to the feature request?

No response

Description

If you're using an enum as the switch value, you cannot guarantee that all properties of that enum have been checked. You can do this with a normal TS switch case by checking that the value is a never in the default case. This won't be possible in NgSwitch, but you can still do a runtime check to make sure each of the enum's values have been checked.

Proposed solution

Create a CheckExhaustive directive. Store the ngSwitchCases within NgSwitch and allow the CheckExhaustive directive to check the enum against the stored switch cases. Throw an error if a case is missed.

Alternatives considered

I tried making my own external directive and tried using the _caseCount value to compare the number of enum properties to. The problem there was that I could potentially add multiple of the same switch cases, which make the count unreliable to evaluate whether it exhaustively checks all the properties of the enum.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: commonIssues related to APIs in the @angular/common packagefeatureIssue that requests a new featurefeature: insufficient votesLabel to add when the not a sufficient number of votes or comments from unique authors

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions