Skip to content

@switch exhaustiveness does not work for @for loop nad @if variables #67406

@pkurcx

Description

@pkurcx

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

No

Description

Summary
@switch exhaustiveness checks do not work when switching on a variable introduced by @for or @if.

Example

@for (nested of nesteds; track nested) {
  @switch (nested.type) { 
    @case ('alpha') { 
      <span>{{ nested.alpha }}</span> 
    } 
    @case ('beta') { 
      <span>{{ nested.beta }}</span> 
    } 
    @default never; 
  }
}

@if (optional.maybe; as union) {
  @switch (union.type) { 
    @case ('alpha') { 
      <span>{{ union.alpha }}</span> 
    } 
    @case ('beta') { 
      <span>{{ union.beta }}</span> 
    }
    @default never; 
  }
}

Expected behavior
@switch exhaustiveness should work for @for loop / @if variables (including their discriminant fields), the same way it works for direct const references, when the switched value is a finite/discriminated union.

Actual behavior
The compilator reports an error.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-4ticm6ar

Please provide the exception or error you saw

✘ [ERROR] TS2322: Type 'any' is not assignable to type 'never'. [plugin angular-compiler]

✘ [ERROR] TS2339: Property 'kind' does not exist on type 'never'. [plugin angular-compiler]

Please provide the environment you discovered this bug in (run ng version)

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI       : 21.2.0
Angular           : 21.2.0
Node.js           : 24.13.0
Package Manager   : bun 1.3.10
Operating System  : darwin arm64

┌──────────────────────────────────┬───────────────────┬───────────────────┐
│ Package                          │ Installed Version │ Requested Version │
├──────────────────────────────────┼───────────────────┼───────────────────┤
│ @angular-devkit/build-angular    │ 21.2.0            │ 21.2.0            │
│ @angular-devkit/core             │ 21.2.0            │ 21.2.0            │
│ @angular-devkit/schematics       │ 21.2.0            │ 21.2.0            │
│ @angular/animations              │ 21.2.0            │ 21.2.0            │
│ @angular/cdk                     │ 21.2.0            │ 21.2.0            │
│ @angular/cli                     │ 21.2.0            │ 21.2.0            │
│ @angular/common                  │ 21.2.0            │ 21.2.0            │
│ @angular/compiler                │ 21.2.0            │ 21.2.0            │
│ @angular/compiler-cli            │ 21.2.0            │ 21.2.0            │
│ @angular/core                    │ 21.2.0            │ 21.2.0            │
│ @angular/elements                │ 21.2.0            │ 21.2.0            │
│ @angular/forms                   │ 21.2.0            │ 21.2.0            │
│ @angular/language-service        │ 21.2.0            │ 21.2.0            │
│ @angular/localize                │ 21.2.0            │ 21.2.0            │
│ @angular/material                │ 21.2.0            │ 21.2.0            │
│ @angular/material-luxon-adapter  │ 21.2.0            │ 21.2.0            │
│ @angular/material-moment-adapter │ 21.2.0            │ 21.2.0            │
│ @angular/platform-browser        │ 21.2.0            │ 21.2.0            │
│ @angular/platform-server         │ 21.2.0            │ 21.2.0            │
│ @angular/router                  │ 21.2.0            │ 21.2.0            │
│ @angular/service-worker          │ 21.2.0            │ 21.2.0            │
│ @schematics/angular              │ 21.2.0            │ 21.2.0            │
│ ng-packagr                       │ 21.2.0            │ 21.2.0            │
│ rxjs                             │ 7.8.2             │ 7.8.2             │
│ typescript                       │ 5.9.3             │ 5.9.3             │
│ vitest                           │ 4.0.18            │ 4.0.18            │
└──────────────────────────────────┴───────────────────┴───────────────────┘

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: compilerIssues related to `ngc`, Angular's template compiler

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions