You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
interfaceFoo{foo: string;}interfaceBar{bar: string;}functiongetFoo(): Foo{return{foo: "foo"};}functiongetBar(): Bar{return{bar: "bar"};}// return type of fubar should be Foo, not Foo | Bar. TS should not infer trully unreachable codefunctionfubar(){constfoo: Foo=getFoo();constbar: Bar=getBar();returnfoo;returnbar;}functionlogFoo(foo: Foo){console.log(foo.foo);}functionmain(){constmyFubar=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.
The text was updated successfully, but these errors were encountered:
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:
In most cases it's more efficient to just do an early return. This however gives us false information.
🔎 Search Terms
🕗 Version & Regression Information
v4.4.2, also tested in v4.3.5.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Return type of
fubar()
is the union ofFoo | Bar
.🙂 Expected behavior
Return type of
fubar()
should beFoo
, notFoo | Bar
. TS should not infer types from code that is guaranteed to be unreachable.The text was updated successfully, but these errors were encountered: