Type check directive template context
See original GitHub issueš feature request
Relevant Package
Description
Template contexts appear not to be checked.
<ng-container *ngIf="data(); let d">
{{ d.a }}
{{ d.c }}
</ng-container>
There is zero checking for the type of d
.
Example: https://github.com/pauldraper/angular-template-context I would like ng build
to fail.
Describe the solution youād like
Template context types are type checked.
Describe alternatives youāve considered
Donāt use template contexts.
Iāve also tried using Ivy but it seems broken.
ERROR in __ng_typecheck__.ts:5:22 - error TS2304: Cannot find name 'Partial'.
5 const _ctor1: (init: Partial<Pick<i1.NgIf, "ngIf" | "ngIfThen" | "ngIfElse">>) => i1.NgIf = (null!);
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Template type checking - Angular
Infers template context types where configured (for example, allowing correct type-checking of NgFor ); Infers the correct type of $event in component/directive ......
Read more >Angular structural directive context template type checking
There is no problem with the directive. The problem is that the angular language service is not updated yet to infer those types...
Read more >Directive Type Checking - DEV Community ā ā
The ngTemplateContextGuard returns true since this directive will always pass a context of type DemoContext to the template. Now when we useĀ ...
Read more >Type-checking templates in Angular View Engine and Ivy
In this article we'll explore how Angular type-checks templates, ... keep the correct type of the context for the template that this directive...
Read more >Typing the Context Object in Angular Structural Directives
Great, our context has the correct type. The way it works is that Angular will compile our template and create Type Check Blocks....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Angular Ivy type-checking system uses
Partial
andPick
built-in TypeScript types. They are declared inlib.es5.d.ts
which means that you have to provide at leastes5
inlib
option of yourtsconfig.json
file: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
_t3
variable isany
.The
NgIf
directive doesnāt implementngTemplateContextGuard
feature likeNgForOf
does. What could work here is:// cc @JoostK
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:
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!What Iām really having a hard time understanding is why
ng_for_of.ts
is generic, butng_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.Thanks