Luau typechecking complains about dynamically-accessed members in non-table objects

function f(vector: Vector3)
   for i, member in next, {'X', 'Y', 'Z'} do
      local v = vector[member]
      --> Expected type table, got 'Vector3' instead
   end
end

This happens for all non-table objects. The only workaround is to re-cast the object as any or as a table with the appropriate members for that specific read, e.g. v = (vector::any)[member], which is gross.

Luau should try to statically resolve the possible values and then warn if something is incorrect. For example, the above code would be okay but if I added a 'W' to the table it would warn me that not all the possible iterations will satisfy the type checker since vector has no W key.

If it can’t statically resolve the values it should try resolving the types. For example, the above code would work but if I added a 100 to the table it would warn me that vector only has string keys and not all iterations would function correctly.

3 Likes

Thanks for the report! We’ve filed a ticket to our internal database and we’ll follow up when we have an update for you.

4 Likes

Thanks for the report!

We’re aware of this issue. We may fix this at some point in the future, but we’ve decided that we can’t prioritize it right now.

Your best bet is either to manually unroll the loop or leave out the Vector3 annotation.