Skip to content

non-null assertion operator (!) causes runtime exception #48742

Closed

Description

@kaisnb

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

Yes

Description

The following template expression ((test$ | async)?.prop1).prop2 with test$ being defined as test$: Observable<any> = of(null) does not throw any runtime exception. However when a non-null assertion operator is added like this ((test$ | async)?.prop1)!.prop2, suddenly a runtime exception is thrown as expected: ERROR Error: Cannot read properties of null (reading 'prop2').

This is unexpected for me since I would already expect a runtime exception without the non-null assertion operator ! and because I believed until today that the non-null assertion operator is fully erased during compile time and does not emit to the transpiled JavaScript code.

A second unintuitive behavior that I discovered while reproducing the error in Stackblitz, was a compile-time error of the expression (test$ | async)?.prop1 with test$ being defined as test$ = of(null). I would not expect the compilation to fail since TypeScript strict mode is not enabled and hence the Angular Template compiler should only show an error in the template but not fail during compilation. Second, the emitted error is Object is possibly 'null'. which is wrong since a ? safe navigation operator is used. If it's correct that the compilation fails with an error, then the expected error would be Property 'prop1' does not exist on type 'never'.. All of the described issues in this paragraph do not appear when we just remove the Observable & AsyncPipe and defined test$ as test$ = null and the expression as test$.prop1. In this case, everything works as expected.

I am not sure if the issues are related, but I expect so. I am sorry if this only appears as a bug to me due to missing knowledge. Please also point out of there is a duplicate issue that I can read and close this instead.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-nubcji?file=tsconfig.json,src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 14.2.10
Node: 16.19.0
Package Manager: npm 8.19.3 
OS: darwin arm64

Angular: 14.2.12
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router, service-worker

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.1500.4
@angular-devkit/build-angular      14.2.10
@angular-devkit/core               15.0.4
@angular-devkit/schematics         14.2.10
@angular/cdk                       14.2.7
@angular/cli                       14.2.10
@angular/flex-layout               14.0.0-beta.41
@angular/google-maps               14.2.7
@angular/material                  14.2.7
@angular/material-moment-adapter   14.2.7
@schematics/angular                14.2.10
rxjs                               7.5.7
typescript                         4.8.4
webpack                            5.75.0

Anything else?

The related Stackblitz shows parts of the issues and must be forked and modified using the code in the description to see all issues.

Activity

  1. JeanMeche commented on Jan 16, 2023

    @JeanMeche
    Member

    An even simplier example triggers the error : {{ $any(null)?.foo!.bar }} while {{ $any(null)?.foo.bar }} is fine !

  2. added
    area: compiler
    on Jan 18, 2023
  3. added this to the needsTriage milestone on Jan 18, 2023
  4. self-assigned this
    on Jan 22, 2023
  5. added a commit that references this issue on Jan 22, 2023
    1d6aad4
  6. added a commit that references this issue on Jan 25, 2023
    171b4d4
  7. added a commit that references this issue on Feb 1, 2023
    5a70d3c
  8. kaisnb commented on Feb 15, 2023

    @kaisnb
    Author

    Thanks for addressing and fixing the above described problem. I just verified the fix and it works correctly. However in my issue I was describing two different problems and I wanted to let you know that the second compilation problem is still present. See a summary of the issue here:

    code strict mode compilation error expected
    test = null; <div *ngIf="test?.prop1"></div> true Property 'prop1' does not exist on type 'never'. ✅
    test = null; <div *ngIf="test?.prop1"></div> false ✅
    test$ = of(null); <div *ngIf="(test$ | async)?.prop1"></div> true Property 'prop1' does not exist on type 'never'. ✅
    test$ = of(null); <div *ngIf="(test$ | async)?.prop1"></div> false Object is possibly 'null'. ❌

    So in short what happens is that if I use a template expression like (test$ | async)?.prop1 with test$ defined like test$ = of(null) I get a compilation error Object is possibly 'null'.. My expecation is to not get a compilation error at all unless I turn on strict mode by setting strict: true in the tsconfig.json and then the thrown error is even wrong since I am using an ? safe-access operator. Above I added some other examples that work as expected and show why the observed result is inconsistent or unexpected.

    @crisbeto @jessicajaniuk

    P.S.: Let me know if I should rather open a new issue instead of reopening this one.

  9. angular-automatic-lock-bot commented on Mar 18, 2023

    @angular-automatic-lock-bot

    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.

  10. locked and limited conversation to collaborators on Mar 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: compilerstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions