Skip to content

Finally is not implemented correctly in all cases #63583

Description

@Mudloop

🔎 Search Terms

finally switch

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://tsplay.dev/Nnz7dN

💻 Code

let y = false
function test1(x: boolean): number {
    try {
        switch (x) {
            case true: return 1
            case false: return 0
        }
    } finally { y ||= true; }
}
// Function lacks ending return statement and return type does not include 'undefined'.
let y = false
function test2(x: boolean): number {
    try {
        switch (x) {
            case true: return 1
            case false: return 0
        }
        return 0;
    } finally { y ||= true; }
}
// Unreachable code detected.

🙁 Actual behavior

Typescript complains about the function not always returning something, even though it does. But when adding an extra return statement, it doesn't like that either.

🙂 Expected behavior

The contents of the finally block should not affect the return type.

Additional information about the issue

Note that if y is only read or written to, it works, but when doing both, things break.

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions