This is pretty different from what you want but it’s the closest I can get to something that looks somewhat okay?
local foo = {
bar = {
baz = 1
}
}
function qux<T>(foobar: string): T
-- might need to cast to 'any' here
return foo[foobar]
end
local value: typeof(foo.bar) = qux("bar")
-- or without assigning to variable
(qux("bar") :: typeof(foo.bar)).baz = 1
In any case, I think the only way to get autocomplete working would be to explicitly cast to the type you want after you call the function like I did above.