luau icon indicating copy to clipboard operation
luau copied to clipboard

Type incorrectly inferred in table when using function result as key

Open ketrab2004 opened this issue 2 years ago • 1 comments

image

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.

ketrab2004 avatar Dec 01 '22 11:12 ketrab2004

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
}

vegorov-rbx avatar Dec 02 '22 12:12 vegorov-rbx