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

Type widened prematurely in catch #57982

Closed
haines opened this issue Mar 28, 2024 · 2 comments
Closed

Type widened prematurely in catch #57982

haines opened this issue Mar 28, 2024 · 2 comments
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@haines
Copy link

haines commented Mar 28, 2024

πŸ”Ž Search Terms

"type widening", "try-catch"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about try-catch

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240328#code/IYZwngdgxgBAZgV2gFwJYHsIwCboO4QA26w2AFCOggE5QCmAXDCMtahAOYwA+MASnRAAHTCDoBKJgAVq6ALaoxAHhZtOAPhgBvAFAwYqODDLIwQuuiOUa9GAF4HMAESr2HJ+O179MVmC8+Pta0dPYwwHjAqMjwdMhQABYUVCHiANze+gC+MFDA8QnGdNSy1J66gfrB9BkA9LWBAHoA-Jk+yAmyeDDFpRmBWd6D3tRxNFgRUTHVdAB0yHQAHshk6TpZQA

πŸ’» Code

async function download(source: string | Response): Promise<string> {
  if (typeof source === "string") {
    try {
      source = await fetch(source);
    } catch (error) {
      source;
//    ^? (parameter) source: string | Response
      throw error;
    }
  }

  return await source.text();
}

πŸ™ Actual behavior

When reassigning a variable inside a try block, the type of the variable is widened inside the catch block even if the assignment cannot have taken place if an error was thrown. In this example the source can only be a string inside the catch block, but the compiler is treating it as string | Response.

πŸ™‚ Expected behavior

The type of the variable is only widened if the assignment could have taken place before the error was thrown.

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label Mar 28, 2024
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Mar 28, 2024

We don't do any analysis of what operations "could"/"could not" have generated exceptions, including the special case of zero operations, since this comes up fairly rarely, is expensive, and would cause some "Well it works if I hold it this way but not this way" confusion where people assume that operations which actually can throw (property access, etc) cannot.

@haines
Copy link
Author

haines commented Mar 28, 2024

Fair enough!

@haines haines closed this as not planned Won't fix, can't repro, duplicate, stale Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

2 participants