Ivy template type checking doesn't narrow types in output positions
See original GitHub issue🐞 bug report
Scope
@angular/compiler-cli Specifically: Ivy template type checking
Is this a regression?
No.
Description / Reproduction
Given a template such as
<some-component
*ngIf="myVar"
[anInput]="myVar.otherVal"
(anOutput)="myFunc(myVar)"
></some-component>
And the owning component class contains
public myVar?: {otherVal: number};
public myFunc(aDefinedVal: {otherVal: number}) {}
The type checker will correctly know that myVar.otherVal
is a safe operation under strictNullChecks
because of the *ngIf
, but the checker will incorrectly throw an error on the myFunc(myVar)
, claiming that myVar
is of type {otherVal: number} | undefined
which is not assignable to type {otherVal: number}
(diagnostic code NG2345
).
🌍 Environment
“@angular/common”: “9.0.0-rc.11”, “@angular/compiler”: “9.0.0-rc.11”, “@angular/compiler-cli”: “9.0.0-rc.11”, “@angular/core”: “9.0.0-rc.11”,
Compiled using Bazel and --define=angular_ivy_enabled=True
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Type-checking templates in Angular View Engine and Ivy
In this article we'll explore how Angular type-checks templates, review the difference between View Engine and Ivy type-checking and break down the process ......
Read more >Can I do type-narrowing in an Angular template?
There is a way in Angular Ivy to allow type narrowing in a template with strict template checking. It's a little hacky but...
Read more >Template type checking - Angular
Infers the correct type of local references to DOM elements, based on the tag name (for example, the type that document.
Read more >Angular 9's Best Hidden Feature: Strict Template Checking
Find and report more errors than ever with Angular 9's Ivy compiler, strict template checking.
Read more >Directive Type Checking - DEV Community
Typescript enforces strict types and helps us make our codebase more resilient. Unfortunately, custom directives are not completely typed ...
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 Free
Top 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
I dropped the following test into packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts to verify the issue.
The issue happens both with and without
strictOutputEventTypes
Hi @DavidANeil, thanks for the report and the testcase! I have a PR that implements Alex’s proposal to repeat the template guards within the event handlers, so their narrowing effect will be in effect.