Skip to content

Guaranteed unreachable code should not be used to infer type #45746

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
anddoutoi opened this issue Sep 6, 2021 · 2 comments
Closed

Guaranteed unreachable code should not be used to infer type #45746

anddoutoi opened this issue Sep 6, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@anddoutoi
Copy link

Bug Report

TS should not infer type from code that is guaranteed to be unreachable. This is mainly a DX issue that gives the developer false information from return types that has been temporarily changed during spiking or when a developer just want to test something out.

Scenario is that a function returns a type and you want to find out if what happens if you would change that return type. So you add a new return statement above the old one, exiting the function earlier without touching the old code.

Workarounds is the obvious:

  • remove the code
  • comment out the code

In most cases it's more efficient to just do an early return. This however gives us false information.

🔎 Search Terms

  • Developer experience
  • Unreachable code
  • Union types

🕗 Version & Regression Information

v4.4.2, also tested in v4.3.5.

⏯ Playground Link

Playground link with relevant code

💻 Code

interface Foo {
    foo: string;
}

interface Bar {
    bar: string;
}

function getFoo(): Foo {
    return {
        foo: "foo"
    };
}

function getBar(): Bar {
    return {
        bar: "bar"
    };
}

// return type of fubar should be Foo, not Foo | Bar. TS should not infer trully unreachable code
function fubar() {
  const foo: Foo = getFoo();
  const bar: Bar = getBar();

  return foo;

  return bar;
}

function logFoo(foo: Foo) {
    console.log(foo.foo);
}

function main() {
    const myFubar = fubar();

    // myFubar is Foo | Bar now, something it can never be in this case!
    
    logFoo(myFubar);
}

🙁 Actual behavior

Return type of fubar() is the union of Foo | Bar.

🙂 Expected behavior

Return type of fubar() should be Foo, not Foo | Bar. TS should not infer types from code that is guaranteed to be unreachable.

@MartinJohns
Copy link
Contributor

Duplicate of #26914. TypeScript gets funky in unreachable code.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label Sep 7, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

4 participants