-
Notifications
You must be signed in to change notification settings - Fork 25.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type check in @if (and *ngIf) causes compiler errors while linter passes #59215
Comments
It looks like we're hitting a bug in TypeScript. The compiler produces the following code to check the template:
It seems like the parentheses are throwing off TypeScript's narrowing. This has happened in the past, see microsoft/TypeScript#56030. |
I've filed an issue with TypeScript: microsoft/TypeScript#60784 |
The issue to track is microsoft/TypeScript#42203 (above was closed as duplicate) |
Please correct me if I’m wrong, but while the linked TypeScript issue would solve this problem, it’s the Angular compiler that generates this seemingly incorrect TypeScript code, isn’t it? Therefore, we could fix it. And since it prevents developers from proper type guarding in HTML, I feel like we should. |
We generate parentheses around expressions to ensure that the comments before them are associated by the TS compiler correctly and we add comments before expressions in order to map errors back to their source location. That being said, I think we should fix it on our end, because the TS issue has been open for a long time and there hasn't been any traction. We can resolve it by generating the |
A PR is expected soon on TS's side. That would fix it when we support 5.7 (in 19.1 and when the fix landed in a TS patch). |
PR in microsoft/TypeScript#60928. This is very unlikely to land in a TS 5.7 patch; it's mostly only recent regressions that are fixed in TS patches. So TS 5.8 at the earliest. I suppose we could address this in the TCB as well, the parenthesis may not be strictly needed (they are at times to ensure proper diagnostic ranges, but that's not necessary in all cases). |
We can do this: if (((typeof x) === ('string') && (typeof x === 'string' /* D:ignore */))) i.e. emit twice and using |
Fitting this into our current system might be tricky since each |
Which @angular/* package(s) are the source of the bug?
compiler
Is this a regression?
Yes
Description
I am using @if (with the same behavior observed for *ngIf) to check a variable’s type. While the TypeScript linter accepts this type check without issues, the Angular compiler (ng build and ng serve) fails to compile.
However, if I replace the inline type check with a TypeScript type-guarding function, everything works as expected.
I don’t see a clear difference between the two approaches, so I suspect this might be a bug.
Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/edit/stackblitz-starters-lk71pysu?file=src%2Fmain.ts&view=editor
Please provide the exception or error you saw
Please provide the environment you discovered this bug in (run
ng version
)Anything else?
No response
The text was updated successfully, but these errors were encountered: