luau
luau copied to clipboard
Type incorrectly inferred in table when using function result as key
something
and bar
are ok, but name: differentFunc
isn't because it doesn't match name: func
. Even though it doesn't need to.
Doing name: func
without the getName()
function does work of course.
Our current type inference is greedy and will commit to the first table indexer value type as soon as it sees one. I would not expect this to be fixed any time soon, we may have improvements for this somewhere in the middle of next year with a new solver.
As a workaround, provide a type annotation for tableOfThings
before assignment with [string]: any
or [string]: (...any) -> ...any
indexer like:
type Things = {
something: boolean,
foo: string,
[string]: (...any) -> ...any
}