Title: Types Not Properly Infered When Using @Switch With A Generic Interface · Issue #60567 · angular/angular · GitHub
Open Graph Title: Types Not Properly Infered When Using @Switch With A Generic Interface · Issue #60567 · angular/angular
X Title: Types Not Properly Infered When Using @Switch With A Generic Interface · Issue #60567 · angular/angular
Description: Which @angular/* package(s) are the source of the bug? common Is this a regression? Yes Description Issue When Using @let offerDetails = offerDetails$ | async; in combination with the ContentCache
Open Graph Description: Which @angular/* package(s) are the source of the bug? common Is this a regression? Yes Description Issue When Using @let offerDetails = offerDetails$ | async; in combination with the ContentCache<...
X Description: Which @angular/* package(s) are the source of the bug? common Is this a regression? Yes Description Issue When Using @let offerDetails = offerDetails$ | async; in combination with the ContentCache&...
Opengraph URL: https://github.com/angular/angular/issues/60567
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Types Not Properly Infered When Using @Switch With A Generic Interface","articleBody":"### Which @angular/* package(s) are the source of the bug?\n\ncommon\n\n### Is this a regression?\n\nYes\n\n### Description\n\n## Issue\nWhen Using `@let offerDetails = offerDetails$ | async;` in combination with the `ContentCache\u003cT>` interface the @switch within the template is unable to infer the appropriate interface.\n\n## Code to Replicate\n\nmodels.ts\n```\nexport enum DiscountType {\n PERCENTAGE_DISCOUNT = \"PERCENTAGE_DISCOUNT\",\n PRICE_DISCOUNT = \"PRICE_DISCOUNT\",\n FREE_TEXT_DISCOUNT = \"FREE_TEXT_DISCOUNT\"\n}\n\nexport interface Vendor {\n name: string;\n logoUrl: string;\n}\n\nexport interface PercentageDiscount {\n type: DiscountType.PERCENTAGE_DISCOUNT;\n discount: {\n percentageOff: number;\n };\n}\n\nexport interface PriceDiscount {\n type: DiscountType.PRICE_DISCOUNT;\n discount: {\n oldPrice: number;\n newPrice: number;\n };\n}\n\nexport interface FreeTextDiscount {\n type: DiscountType.FREE_TEXT_DISCOUNT;\n discount: {\n text: string;\n };\n}\n\nexport type Discount = PriceDiscount | PercentageDiscount | FreeTextDiscount;\n\nexport interface Reward {\n title: string;\n description?: string;\n vendor: Vendor;\n discount: Discount;\n expiry: number;\n productImgUrl: string;\n productUrl: string;\n discountCodeInfo: {\n code: string;\n claimDate: number;\n };\n}\n\nexport enum ContentState {\n LOADING = \"LOADING\",\n LOADED = \"LOADED\",\n ERROR = \"ERROR\",\n EMPTY = \"EMPTY\"\n}\n\nexport interface ContentCache\u003cT> {\n state: ContentState;\n data?: T;\n error?: { message?: string; params?: any };\n}\n```\n\nngrx selector.ts\n```ts\nexport const getOfferDetails = createSelector(selectGlobalState, (state: GlobalState) => state.offerDetails as ContentCache\u003c{ offer?: Reward,\n discountCode: string}>);\n```\n\ncomponent.ts\n```ts\nimport { Component, inject, OnDestroy } from \"@angular/core\";\nimport { Store } from \"@ngrx/store\";\nimport { ContentCache, ContentState } from \"../../models/cache-content.model\";\nimport { map, Observable, tap } from \"rxjs\";\nimport { getOfferDetails } from \"../../store/selectors/global.selector\";\nimport { CommonModule } from \"@angular/common\";\nimport { Discount, DiscountType, Reward } from \"../../models/offer-details.model\";\nimport { Router } from \"@angular/router\";\nimport { GlobalActions } from \"../../store/actions/global.actions\";\n\n@Component({\n selector: \"mr-offer-details\",\n imports: [CommonModule],\n templateUrl: \"./offer-details.component.html\",\n styleUrl: \"./offer-details.component.scss\"\n})\nexport class OfferDetailsPage implements OnDestroy {\n router = inject(Router);\n contentState = ContentState;\n DiscountType = DiscountType;\n\n offerDetailsState$: Observable\u003cContentState | undefined>;\n offerDetails$: Observable\u003cContentCache\u003c{ offer?: Reward; discountCode: string }> | undefined>;\n offerDetailsDiscount?: Discount\n \n\n constructor(readonly store: Store) {\n this.offerDetailsState$ = this.store.select(getOfferDetails).pipe(\n map((offerDetails) => offerDetails?.state),\n tap((state) => {\n if (state == ContentState.EMPTY) this.router.navigate([\"/home\"]);\n })\n );\n this.offerDetails$ = this.store.select(getOfferDetails).pipe(tap((data) => {\n // For The Hacky Solution\n this.offerDetailsDiscount = data?.data?.offer?.discount\n }));\n }\n ngOnDestroy(): void {\n this.store.dispatch(GlobalActions.clearOfferDetails());\n }\n}\n```\n## Broken Template\n\n```html\n@let offerDetails = offerDetails$ | async;\n\n\u003cdiv class=\"offer-details-container\">\n @if(offerDetails){\n @switch (offerDetails.data?.offer?.discount?.type) {\n @case(DiscountType.FREE_TEXT_DISCOUNT){\n \u003ch1>{{ offerDetails.data?.offer?.discount?.discount.text }}\u003c/h1>\n }\n }\n }\n\u003c/div>\n```\n\n\n\n\n## Hacky Solution\n\ncomponent.html\n```html\n\u003cdiv class=\"offer-details-container\">\n @if(offerDetailsDiscount){\n @switch (offerDetailsDiscount.type) {\n @case(DiscountType.FREE_TEXT_DISCOUNT){\n \u003ch1>{{ offerDetailsDiscount.discount.text }}\u003c/h1>\n }\n }\n }\n\u003c/div>\n```\n\n\n### Please provide a link to a minimal reproduction of the bug\n\n_No response_\n\n### Please provide the exception or error you saw\n\n```true\nWhen Using `@let offerDetails = offerDetails$ | async;` in combination with the `ContentCache\u003cT>` interface the @switch within the template is unable to infer the appropriate interface. (code available in issue)\n```\n\n### Please provide the environment you discovered this bug in (run `ng version`)\n\n```true\nAngular CLI: 19.2.4\nNode: 20.18.1\nPackage Manager: npm 11.0.0\nOS: win32 x64\n\nAngular: 19.2.3\n... animations, common, compiler, compiler-cli, core, forms\n... platform-browser, platform-browser-dynamic, router\n\nPackage Version\n---------------------------------------------------------\n@angular-devkit/architect 0.1902.4\n@angular-devkit/build-angular 19.2.4\n@angular-devkit/core 19.2.4\n@angular-devkit/schematics 19.2.4\n@angular/cdk 19.2.4\n@angular/cli 19.2.4\n@angular/material 19.2.4\n@schematics/angular 19.2.4\nrxjs 7.8.2\ntypescript 5.7.3\nzone.js 0.15.0\n```\n\n### Anything else?\n\n_No response_","author":{"url":"https://github.com/EdinGrech","@type":"Person","name":"EdinGrech"},"datePublished":"2025-03-26T16:49:35Z","url":"https://github.com/angular/angular/issues/60567"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:12db2e78-e787-c5fc-a010-1ee798c25ad9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CA5C:25598F:193D6BC:214E8F0:6B014539 |
| html-safe-nonce | 6396ec13ac430d32ad03079c8fb9a70fbf64cae0ba7a7133a4bd0633cfa3135e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQTVDOjI1NTk4RjoxOTNENkJDOjIxNEU4RjA6NkIwMTQ1MzkiLCJ2aXNpdG9yX2lkIjoiMjQ2NzA1MTg4OTc1MDAwMDk1MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ac957a85f413d14d5a038937da4afea6295c67239fa467bcad9a4c647580dc4b |
| hovercard-subject-tag | issue:2950241805 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/60567/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a6e284dcf54acfdc43a6c4ca19b5142af9cb9c3bee2f83aa0b0a270f96443d87/angular/angular/issues/60567 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a6e284dcf54acfdc43a6c4ca19b5142af9cb9c3bee2f83aa0b0a270f96443d87/angular/angular/issues/60567 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? common Is this a regression? Yes Description Issue When Using @let offerDetails = offerDetails$ | async; in combination with the ContentCache<... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | EdinGrech |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width