Skip to content
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

[TypeScript] 5.4.5 - if(true) {return -1} throws off control-flow analysis #58199

Closed
riven8192 opened this issue Apr 15, 2024 · 1 comment
Closed
Labels
Duplicate An existing issue was already created

Comments

@riven8192
Copy link

riven8192 commented Apr 15, 2024

πŸ”Ž Search Terms

none

πŸ•— Version & Regression Information

All tested versions of TypeScript (tested from 3.3.3 until 5.4.5 in the playground)

⏯ Playground Link

https://www.typescriptlang.org/play?target=9&jsx=0&module=7&ts=5.4.5#code/JYOwLgpgTgZghgYwgAgLIQM4bgcxQbwFgAoZZBAe3AnAH4AuZDMKUHAbhIF8SSEAbOFjSZseAMIUAtgAcqNMMiKlkU0bggBCRuiwbOxEmQAmAVylSAngAoAlIxDmARtCVGyyAPSfkAOQB0AEL+jHBgkLJgGMhgFDEQshRQcKz8lsjGwNhO-ChqYAAWFMYAtMCy-LwqZMAw1iymELZu1R7e5HCmGJiM0FBJyAAqAMoATACsAMyjjADyTgBWEAiKWchyWMA56QDkpiDGEDCgEMY7-u4eXj5XV1TI-CfIAO7AhcighwAeszAANMgIAA3BRFUw4AofRQCCApNIZCiYS5tG63MhwcgFZYAa2QMCSjE0AF4ich9odjiBTh8QDEsQ8ni58VAUG8SABIK4ssCmKC0koARgMVx4rWR3lq9SgjWQQmQTgoFFycBAzXw1xeSWx0UpEHFN25vP5Qv1oqu+slApJRIFao1zy1OpO+rIhr5yEFwpRovZLsKKCSwBwoDg-FUEEKxTKFWRksKWX8aj0eH8lGo4GQxNJ5KOJ2MauRXIjRrpCaTYggqaokHA-k+EB+dR2JRbO1sXrIZo8yLdxq9otFfCoGCVlf4FBw1h2cCcCHoO2QAGpkFTnsgACJhCB2fx4MCDcrb2y2IA

πŸ’» Code

interface Message {
  content?: string;
}

class MessageComponent {
  message!: Message;

  dummy(): number {
    // N.B.: attempts to temporarily disable method-impl

    if(true) {
      // causes: error TS2532: Object is possibly 'undefined'.
      //         on line with indexOf, eventhough it clearly does
      //         a check for: !== undefined in the line before it
	    return -1;
    }
    
    //if(true as boolean) { // works fine
    //  return -1;
    //}
    
    //if(1===1) { // works fine
    //  return -1;
    //}
	
    // the original method-impl
    if(this.message.content !== undefined) {
        return this.message.content.indexOf('---');
    }
    
    return -1;
  }
}

πŸ™ Actual behavior

It throws an compile-time error that in the following (unreachable) code:

    if(this.message.content !== undefined) {
        return this.message.content.indexOf('---');
    }

the variable this.message.content may be undefined, when attempting to call indexOf.

πŸ™‚ Expected behavior

There probably should be merely a warning of unreachable code, not a blocking compile-time error about a variable potentially being undefined, when one can clearly see it cannot be undefined (whether or not the code was reachable)

Additional information about the issue

No response

@RyanCavanaugh
Copy link
Member

#26914

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants