I noticed somewhere in my code that the result of a ternary operator triggered an odd error later on when indexing into it.
Trying to minimize the repro case to the minimum size possible produces this:
--!strict
local test = if true then {} else {}
local lest2 = test[1] -- The error is Expected type table, got '{ } | { }' instead.
Note that if you specify test as being of type {}
on the declaration line, then Luau successfully resolves this. This is an easy workaround.
--!strict
local test: {} = if true then {} else {}
local lest2 = test[1] -- no error here
This also does not reproduce with custom named types. If you have a function returning a named type, then the resolver can work out, for example:
--!strict
type foo = {}
function a(): foo return {} end
function b(): foo return {} end
local test = if true then a() else b()
local lest2 = test[1] -- no error here. type of test is '{| |}'
Using foo = {number} resolves to {number}, etc.
This fails with non-named types returned from functions.
--!strict
function a(): {} return {} end
function b(): {} return {} end
local test = if true then a() else b()
local lest2 = test[1] -- The error is Expected type table, got '{| |} | {| |}' instead.
Expected behavior
Luau should understand that result of the ternary operator is still indexable and not show an error or warning here. Note that the # (table count operator) works just fine on these mixed types without producing an error.
A private message is associated with this bug report