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
Currently, the editor infers the return type of test to be unknown (when it should be number), which causes the value + 1 expression afterwards to be considered illegal.
This other snippet also fails:
functiontest(){return1return""}test()+1
This time it's because the return type of test is inferred to be number | string, making the value + 1 expression illegal.
In general, VSCode appears to be inferring the return value of a function with unreachable return statements as a discriminated union of the types of the values being returned in them, even if they are unreachable.
For instance, this final snippet works because the return type of test is inferred to be any (or, rather, number | any which is simplified to any):
functiontest(){return1return""asany}test()+1
VS Code version: Code 1.73.1 (Universal) (6261075646f055b99068d3688932416f2346dd3b, 2022-11-09T02:08:38.961Z)
OS version: Darwin x64 21.5.0
Modes:
Sandboxed: No
System Info
Item
Value
CPUs
Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz (12 x 2600)
This inference is still sound and I think there's a reasonable argument to be made that code like this is always transitory, so the question is "What is the final state of this code going to be?" - of which there are obviously two possible answers. With the current behavior, in the case where the second return is removed, then downstream errors go away, and if the second return becomes reachable then the downstream code gets the correct typechecking behavior the whole time. If unreachable returns are ignored then the reverse happens, and that seems worse IMO.
in the case where the second return is removed, then downstream errors go away
Devil's advocate position is that the wider return type, while sound, has potentially detrimental downstream effects, e.g. being forced to write type guards and/or casts and then forgetting to remove them once the second return goes away. Not a Defect is definitely the appropriate response because there's no right or wrong answer here.
Type: Bug
This snippet does not currently work:
Currently, the editor infers the return type of
test
to beunknown
(when it should benumber
), which causes thevalue + 1
expression afterwards to be considered illegal.This other snippet also fails:
This time it's because the return type of
test
is inferred to benumber | string
, making thevalue + 1
expression illegal.In general, VSCode appears to be inferring the return value of a function with unreachable return statements as a discriminated union of the types of the values being returned in them, even if they are unreachable.
For instance, this final snippet works because the return type of
test
is inferred to beany
(or, rather,number | any
which is simplified toany
):VS Code version: Code 1.73.1 (Universal) (6261075646f055b99068d3688932416f2346dd3b, 2022-11-09T02:08:38.961Z)
OS version: Darwin x64 21.5.0
Modes:
Sandboxed: No
System Info
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
metal: disabled_off
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off
Extensions (26)
A/B Experiments
The text was updated successfully, but these errors were encountered: