Repository navigation
Closed
Labels
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentAn issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: commonIssues related to APIs in the @angular/common packageIssues related to APIs in the @angular/common packagearea: compilerIssues related to `ngc`, Angular's template compilerIssues related to `ngc`, Angular's template compilercompiler: template type-checkingfeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under considerationFeature request for which voting has completed and the request is now under consideration
Milestone
Description
Current behavior
No type narrowing is performed when using NgSwitch and NgSwitchCase.
Expected behavior
Narrow the types used in the NgSwitchCase based on the selection criteria.
See #17953 (comment) and following for more details.
Reacted by Osman Yavuz, Dirk Luijk, Yaroslav Admin, Ycros, Magnus Grindal Bakken, Dylan Van Bommel, AJ Richardson, Ben Elliott, Tobias Becker, Hossam El-Deen and 206 more
Reacted by David Silva, Klemen Oslaj, ZHAO Jin-Xiang, Ali, Edu Pereda, Amit Beckenstein, Mullot, Adam Wamai Egesa, Julio Vizcaíno Molina, Lucas and 5 more
Metadata
Metadata
Labels
P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentAn issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: commonIssues related to APIs in the @angular/common packageIssues related to APIs in the @angular/common packagearea: compilerIssues related to `ngc`, Angular's template compilerIssues related to `ngc`, Angular's template compilercompiler: template type-checkingfeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issuesfeature: under considerationFeature request for which voting has completed and the request is now under considerationFeature request for which voting has completed and the request is now under consideration
Activity
dirkluijk commentedon Feb 1, 2018
Looking forward to see type guard support in language-service as well!
My simple use case:
Use case 1
Use case 2
Are both use cases covered?
waterplea commentedon Sep 9, 2019
This would be a great addition, having an issue similar to case 2 that @dirkluijk posted.
amitport commentedon Oct 31, 2019
@chuckjaz any updates? Anyone working on this? Thanks
dirkluijk commentedon Jan 21, 2020
Will this be solved in Ivy?
SchnWalter commentedon Jan 21, 2020
No, this isn't solved by Ivy, if anything, Ivy will cause more people to realize how badly they need
ngSwitchto support type narrowing.klemenoslaj commentedon Jun 24, 2020
This would really clean up a lot of template code. How come this is such a low priority?
Lonli-Lokli commentedon Jan 21, 2021
It has 63 upvotes so seems like pretty needed feature.
13 remaining items
flevi29 commentedon Jun 16, 2023
I've been wondering if this is true,
switchwould have to re-run each time too, it needs to check which template to render. So a nested "if-else-if" ugly depressing monstrosity should be just as good. Of course there's some added overhead but it should be absolutely minimal. The biggest headache is the resulting monster lasagna code. It's so much more verbose and hard to read than it has to be.Jrubzjeknf commentedon Jun 16, 2023
Check discussion #50719, header
switch block.stealthAngel commentedon Aug 25, 2023
Although I like the answer of @dirkluijk There is a much simpler solution:
In the component i'm casting it to the shape.
see my git repository for an example:
baratgabor commentedon Aug 30, 2023
One technique that can help:
maxime1992 commentedon Aug 31, 2023
@baratgabor this is about
NgSwitchandNgSwitchCase. Nothing will be enforced on the template side as far as I know with your code abovejpike88 commentedon Oct 20, 2023
I've banned use of ngSwitch in our projects because not only does it not have type narrowing, there is zero type awareness in the case settings meaning any old thing can be used in the cases and we don't know about the problem until a bug happens.
juane1000 commentedon Oct 20, 2023
I think this may be locked going forward because the new template flow control syntax will enable type narrowing. NgSwitch and NgIf will be sunset according to previous angular team comments on their stream. Existing codebases will have to migrate if they want the type safety in the templates.
jpike88 commentedon Oct 20, 2023
Does anyone know how long roughly that RFC is going to take to be implemented? It still seems to be in early stages...
JeanMeche commentedon Oct 20, 2023
The new control flow is pretty much implemented an available in the current RC.
It will be available as a developer preview in v17 (release in early november).
jpike88 commentedon Oct 20, 2023
Thanks. I'll note that I'm just trying it out now on the current RC (I added the appropriate stuff to
angularCompilerOptions), and neither vscode (typescript and angular extension on latest) orng buildis flagging any type related issues. In that screenshot,isDebugvariable is a boolean... just an FYIjpike88 commentedon Oct 23, 2023
Sorry that wasn't very clear. What I meant is that I expected there to be some sort or type narrowing to be happening in those case statements as they don't match the type of the
isDebugvariableedit: looks like it's appropriate for me to open issues about this. ill start doing that
alxhub commentedon Dec 15, 2023
Closing as we have no plans to support coordinated type-checking of multiple structural directives, but
@switchdoes support type-checking (it's about time!)angular-automatic-lock-bot commentedon Jan 15, 2024
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.