luau icon indicating copy to clipboard operation
luau copied to clipboard

`if then else` assignments fail to simplify unions of the same type

Open ThatOnePuppyGirl opened this issue 2 years ago • 4 comments

local b, c, d, f = true, true, true, true

local a = if b then {} elseif c then {} elseif d then {"a"} elseif f then {"a"} else {}

results in a being of type { } | { } | { } | {string} | {string}, as shown in the photo below: I am using the Inlay Hints setting in luau-lsp, and I have been told this is a Luau issue and to bring it here. image Luau should recognize that T | T == T, or at least attempt something like this.

ThatOnePuppyGirl avatar Sep 16 '23 04:09 ThatOnePuppyGirl

If someone could add the bug tag that would be very helpful, as I don't seem to be able to.

ThatOnePuppyGirl avatar Sep 16 '23 22:09 ThatOnePuppyGirl

As a work-around, I would recommend just helping out the inference by providing local a: {string}.

vegorov-rbx avatar Sep 25 '23 15:09 vegorov-rbx

As a work-around, I would recommend just helping out the inference by providing local a: {string}.

This happens with all data types, I just used those specific values as an example.

ThatOnePuppyGirl avatar Sep 25 '23 15:09 ThatOnePuppyGirl

Just ran into this issue today with something like the following code (yes, what I am doing is hacky):

local module = if string.sub(_VERSION, 1, 4) == "Lune" then require("../lib/module") else require(script.Parent.Parent.lib.module)

This creates type errors whenever I try to call functions in my module because it create unions for all of the functions that look something like (() -> ()) | (() -> ()) , which luau doesn't consider to be a function. The workaround does not work in my situation because I don't have access to the type until after the require function is executed. Instead I must expand the statement into a full if statement like so:

local module
if string.sub(_VERSION, 1, 4) == "Lune" then
    module = require("../lib/module")
else
    module = require(script.Parent.Parent.lib.module)
end

A little more cluttered but it works and figures out all of the types correctly.

mxruben avatar Jun 13 '24 03:06 mxruben