Skip to content

This condition will always return 'false' since the types 'true' and 'false' have no overlap #29155

Closed

Description

@txiaocao

**TypeScript Version: "typescript": "^3.2.2"

This is an asynchronous method that controls its pause externally, so it needs to be removed at the right time. However, an error warning was given to indicate that this method is not feasible.

  public async show() {
    this.showing = true;
    while (this.showing) {
      for (const frameKey in DogDto.EffectSaveInfo) {
        // [ts] This condition will always return 'false' since the types 'true' and 'false' have no overlap.
        if (this.showing === false) {
          this.container.removeAllChildren();
          break;
        }
        const frameInfo = DogDto.EffectSaveInfo[frameKey];
        const frameIndex = Number(frameKey);
        const frameBitmap = await BitmapUtils.getBitmap(DogDto.EffetcSaveImageList[frameIndex]);
        frameBitmap.x = -1 * frameInfo.origin.x;
        frameBitmap.y = -1 * frameInfo.origin.y;
        container.removeAllChildren();
        container.addChild(frameBitmap);
        await LarkDate.delay(frameInfo.delay);
      }
    }
  }
  public hide() {
    this.showing = false;
  }

usage

  public async showEffect() {
    this.effectSave.show();
    await LarkDate.delay(3 * 1000);
    this.effectSave.hide();
  }

Activity

  1. weswigham commented on Dec 26, 2018

    @weswigham
    Member

    Looks like control flow needs to be more pessimistic around await.

  2. txiaocao commented on Dec 26, 2018

    @txiaocao
    Author

    Temporarily changed to recursive writing :)

      public async show() {
        this.showing = true;
        const container = this.container;
        const frame = DogDto.EffetcSaveImageList[0];
        const frameInfo = DogDto.EffectSaveInfo[0];
        const bitmap = await BitmapUtils.getBitmap(frame);
        bitmap.x = -1 * frameInfo.origin.x;
        bitmap.y = -1 * frameInfo.origin.y;
        container.addChild(bitmap);
        await this.play();
      }
      public hide() {
        this.showing = false;
      }
      private async play() {
        const container = this.container;
        for (const frameKey in DogDto.EffectSaveInfo) {
          const frameInfo = DogDto.EffectSaveInfo[frameKey];
          const frameIndex = Number(frameKey);
          const frameBitmap = await BitmapUtils.getBitmap(DogDto.EffetcSaveImageList[frameIndex]);
          frameBitmap.x = -1 * frameInfo.origin.x;
          frameBitmap.y = -1 * frameInfo.origin.y;
          container.removeAllChildren();
          container.addChild(frameBitmap);
          if (this.showing === false) {
            this.container.removeAllChildren();
            break;
          }
          await LarkDate.delay(frameInfo.delay);
        }
        if (this.showing === true) {
          await this.play();
        }
      }
  3. ajafff commented on Dec 26, 2018

    @ajafff
    Contributor

    This is basically the same as calling a function, method or callback that could modify a narrowed variable. There should be plenty of duplicate issues, IIRC there was at least one regarding await.

    The proposed solution was using a method to retrieve the value instead of accessing the property directly.

  4. txiaocao commented on Dec 26, 2018

    @txiaocao
    Author

    yes, it work ...

    class Test{
      private status = false;
      public async test(){
        while(this.status){
          if(this.getStatus() === false){
            break;
          }
        }
      }
      private getStatus(){
        return this.status;
      }
    }
  5. trevorr commented on Jun 8, 2022

    @trevorr

    Looks like control flow needs to be more pessimistic around await.

    Why isn't this the correct answer? I understand the workaround, but it doesn't seem like it should be necessary. Not only does narrowing across await cause valid code to be rejected, it doesn't detect potential bugs due to narrowed values changing.

  6. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysis

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions