Luau: Types doesn't support direct indexing

Greetings! I have been testing a while back the Luau: Types feature in Roblox, and already most of all my codes are type-safe. But I’ve run into problems that I’ve let go, but I can’t anymore; and one of the ones that bothers me the most are that:

When specifying the type to a variable that is directly indexing a Workspace object, you cannot access the children (indexing them directly) within that directly indexed object.

Code of reproduction:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Communication: Folder = ReplicatedStorage.Communication
local RemoteFunction: RemoteFunction = Communication.Remote.Functions.EquipTool

RemoteFunction:InvokeClient()

How it looks in the editor:

image

Explorer hierarchy:

image

And the only solution I have found to this problem is to use the WaitForChild() function too many times, and the code would look like this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Communication: Folder = ReplicatedStorage:WaitForChild("Communication") :: Folder
local RemoteFunction: RemoteFunction = Communication:WaitForChild("Remote"):WaitForChild("Functions"):WaitForChild("EquipTool") :: RemoteFunction

RemoteFunction:InvokeClient()

But it is something that I honestly don’t think is the most stable, and especially to be overloading the code of many yields. Even, I have to do a type-casting because if I don’t add it I will get an error that it is an Instance and not a Folder or RemoteFunction.

And I know why this happens, and it is because when specifying a type to a variable, Luau omits the type of the variable value (if we index it directly), and the two seem to be the same, but no, the variable value has its children that are seen in the explorer, while the type assigned to the variable has no children, it only specifies the methods and attributes without any children.

I would like to know if there is a specific Luau: Types method that allows this, as I can’t find anything on the official website. And if there is nothing, then I would like to pass this on to the eyes of Roblox, so they can do something about it. I don’t have permissions to post in the Feature Requests category.

If this is an incorrect category, please inform me privately.

Thank you, Sandy Stone.

Referencing objects that are already in the game, can autocomplete with its properties and its children.

But Luau hates it when directly indexing children if it is type asserted or whatever

image

2 Likes

Dirty fix is to coerce Communication’s type to any.

local RemoteFunction: RemoteFunction = (Communication :: any).Remote.Functions.EquipTool

Proper fix is to use FindFirstChild and WaitForChild. Typechecking assumes property access when direct indexing meanwhile one of the child getter functions should be used for looking for children (ultimately should’ve been the canonical and only way to get children because of namespace conflictions but that ship has long sailed).

2 Likes