Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
txiaocao opened this issue Dec 26, 2018 · 5 comments
Labels
Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis

Comments

@txiaocao
Copy link

**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();
  }
@weswigham weswigham added Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis labels Dec 26, 2018
@weswigham
Copy link
Member

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

@txiaocao
Copy link
Author

txiaocao commented Dec 26, 2018

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();
    }
  }

@ajafff
Copy link
Contributor

ajafff commented Dec 26, 2018

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.

@txiaocao
Copy link
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;
  }
}

@trevorr
Copy link

trevorr commented Jun 8, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis
Projects
None yet
Development

No branches or pull requests

4 participants