Luau type cast / as / refinement

Hi! I’m trying to use the Luau type annotations to write a script. Here’s what I’ve got:

local function add_entry( entry: GuiObject )
end

listObject.ChildAdded:Connect(function(child: Instance)
	if child:IsA("GuiObject") then
		add_entry(child)
	end
end)

Which gives me the warning “Type ‘GuiObject’ could not be converted into ‘Instance’”.

Obviously child is an Instance so it can’t be directly passed to add_entry which expects a GuiObject, so the warning makes sense. But how do I tell Luau that I’ve checked to make sure child is actually a GuiObject so I can pass it to add_entry? There’s no as keyword, and I don’t know how to fix this.

I’ve searched around on Google and here on the dev forums, and as far as I can tell it’s not covered here: Type checking - Luau

2 Likes

I would ask this question in the announcement thread. I’m not sure that refinement works with IsA, although that would be a sensible feature.

1 Like

Sorry for bump!
I don’t know if you fixed/found out by your own, but today they released the assertion operator: ::
In your example you can use it like so:

local function add_entry( entry: GuiObject )
end

listObject.ChildAdded:Connect(function(child: Instance)
	if child:IsA("GuiObject") then
		add_entry( (child :: GuiObject) )
	end
end)

All the best to you!

4 Likes

Hurray! What an amazing update :smiley: Thanks for letting me know

1 Like