Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type guard not being identified when assigned to boolean – strict mode

I have the following code:

The condition is the same in both typeguards, but only the second one works. I’d love to understand why?

export class MessageService {
  private _iframe?: HTMLIFrameElement;

  sendMessage() {
    const appLoaded = !!this._iframe?.contentWindow;

    // why does this not work?
    if (appLoaded) {
        this._iframe.contentWindow.postMessage({}, '*'); // ERROR
    }

    // while this does
    if(!!this._iframe?.contentWindow) {
        this._iframe.contentWindow.postMessage({}, '*');
    }
  }
}

TS Playground link

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks!

>Solution :

Because TypeScript’s flow analysis doesn’t go that far. It doesn’t track the fact that appLoaded being true also means that this._iframe?.contentWindow is also truthy. It’s just a pragmatic limitation of the compiler. In theory, it could do it (until/unless you called a function after the assignment and before using contentWindow, at which point this._iframe and or this._iframe.contentWindow may have changed), but doing that is complex enough that it doesn’t.

You could always do something like this, if you want to avoid repeating the condition:

const wnd = this._iframe?.contentWindow;
// ...
if (wnd) {
    wnd.postMessage({}, '*');
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading