-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Exhaustive @switch type-checking #52107
Copy link
Copy link
Closed
Labels
area: coreIssues related to the framework runtimeIssues related to the framework runtimecompiler: template type-checkingcore: control flowIssues related to the built-in control flow (@if, @for, @switch)Issues related to the built-in control flow (@if, @for, @switch)featureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issues
Milestone
Metadata
Metadata
Assignees
Labels
area: coreIssues related to the framework runtimeIssues related to the framework runtimecompiler: template type-checkingcore: control flowIssues related to the built-in control flow (@if, @for, @switch)Issues related to the built-in control flow (@if, @for, @switch)featureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issues
Which @angular/* package(s) are relevant/related to the feature request?
common
Description
I think it would be a great addition to the type-safety of templates if
@switchwere checked to be exhaustive.Currently, if you have a field with 3 possible values:
and then use a
@switchin the template:@switch (userType) { @case ('admin') { Hello admin } @case ('logged') { Hello user } }the template compiles just fine, whereas the
'anonymous'case is not covered.Proposed solution
It would be great to get a compiler error if a case is missing (when no default is provided of course).
TypeScript would complain about a missing case, if the switch was used to return a value in a function:
The generated TCB could maybe be code along those lines instead of a "simple" switch?
Alternatives considered
I don't think we can work around that easily?