You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Angular Ivy type-checking system uses Partial and Pick built-in TypeScript types. They are declared in lib.es5.d.ts which means that you have to provide at least es5 in lib option of your tsconfig.json file:
@alexzuza thanks for the explanation! :-) Would you be able to open a PR with your proposed changes?
@pauldraper I would be interested to know how you ended up without lib.es5.d.ts, if that is indeed the cause of the Ivy error. The reason why I'm asking is that perhaps we should not assume that these typings are available and have the type checker output its own definitions.
Even without Ivy you can sort of achieve this today (8.1.2), if you use *ngFor instead of *ngIf....
This is quite a clumsy hack - but works to achieve what the OP was trying to do:
export type Product = { sku: string, price: number };
// helper function to take a single product and convert it to the correct type
// this works even for a `*ngTemplateOutletContext` parameter which is why I needed this
coerceProductArray(product: any): Product[]
{
return [ product ]; // creates a single item array
}
Then iterate through this list - (of one item) - and the correct type for product will be available inside. The attempted multiplication of a string below will not be allowed!
<ng-container *ngFor="let product of coerceProductArray(product)">
<!-- Compiles -->
{{ product.sku }}
{{ product.price }}
{{ product.price * 2 }}
<!-- These have squiggles! Yay -->
{{ product.desc }}
{{ product.price + 'cat' }}
{{ product.sku * 2 }}
</ng-container>
What I'm really having a hard time understanding is why ng_for_of.ts is generic, but ng_if.ts isn't. Is it just not possible for *ngIf to use the same mechanism that *ngFor does to achieve this.
What's the reason ng_if isn't a generic type today? Where does Ivy help with this in future - will this problem just go away some day. (And yes - I too was very confused by the comments highlighted in the previous post).
I was about to embark on creating my own *ngIf but I figured it would be a futile exercise until i can understand better what's going on - this seemed to be the closest issue.
Activity
[-]Type check template context[/-][+]Type check directive template context[/+]alexzuza commentedon Jul 15, 2019
Angular Ivy type-checking system uses
PartialandPickbuilt-in TypeScript types. They are declared inlib.es5.d.tswhich means that you have to provide at leastes5inliboption of yourtsconfig.jsonfile:Once you've done, the compiler will be executed without any errors.
Why?
Angular Ivy type-checking system for ngIf only supports ngIf guard but not template context guard.
The generated code for your case looks like:
where the type of
_t3variable isany.The
NgIfdirective doesn't implementngTemplateContextGuardfeature likeNgForOfdoes.What could work here is:
// cc @JoostK
JoostK commentedon Jul 15, 2019
@alexzuza thanks for the explanation! :-) Would you be able to open a PR with your proposed changes?
@pauldraper I would be interested to know how you ended up without
lib.es5.d.ts, if that is indeed the cause of the Ivy error. The reason why I'm asking is that perhaps we should not assume that these typings are available and have the type checker output its own definitions.alexzuza commentedon Jul 15, 2019
@JoostK Will do
feat(ivy): support types narrowing for template context of NgIf
pauldraper commentedon Jul 19, 2019
@JoostK I'm not sure. You can take a look at my rather minimal example.
https://github.com/pauldraper/angular-template-context
Add
"enableIvy": trueto src/tsconfig.json and then runng build.pauldraper commentedon Jul 26, 2019
On the Ivy point, oh, I see. My example has lib: "dom" but not "es5".
Alright, after fixing that, even with Angular Ivy this code still validates.
And based on the documentation for
NgIf.ngTemplateGuard_ngIf, it appears this is supposed to be type checked.https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_if.ts#L219-L227
EDIT: Oh, I see your comments.
simeyla commentedon Aug 10, 2019
Even without Ivy you can sort of achieve this today (8.1.2), if you use
*ngForinstead of*ngIf....This is quite a clumsy hack - but works to achieve what the OP was trying to do:
Then iterate through this list - (of one item) - and the correct type for
productwill be available inside. The attempted multiplication of a string below will not be allowed!What I'm really having a hard time understanding is why
ng_for_of.tsis generic, butng_if.tsisn't. Is it just not possible for*ngIfto use the same mechanism that*ngFordoes to achieve this.What's the reason
ng_ifisn't a generic type today? Where does Ivy help with this in future - will this problem just go away some day. (And yes - I too was very confused by the comments highlighted in the previous post).I was about to embark on creating my own
*ngIfbut I figured it would be a futile exercise until i can understand better what's going on - this seemed to be the closest issue.Thanks
pauldraper commentedon Aug 23, 2019
Interesting. I didn't not know it was possible with ngForOf.
ngIf simply makes no attempt for generic types, so it very unsurprising it is untyped.
fix(common): reflect input type in NgIf context
fix(common): reflect input type in NgIf context (#33997)
angular-automatic-lock-bot commentedon Jan 2, 2020
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.