Instance.FindFirstChild
can return nil, so the return type should be changed to Instance?
.
Page URL: https://create.roblox.com/docs/reference/engine/classes/Instance#FindFirstChild
Instance.FindFirstChild
can return nil, so the return type should be changed to Instance?
.
Page URL: https://create.roblox.com/docs/reference/engine/classes/Instance#FindFirstChild
Issue does not apply to Instance.WaitForChild
.
Same applies to other dynamic returning functions such as FindFirstChildOfClass
.
All of these functions used to return properly, but I cannot pinpoint exactly when it started happening, but my best guess is after the release of the new beta type solver. I switched over to the beta but since then returned back to the normal type solver - unsure if this switching had any effect on it.
Interestingly enough, when typing out, the intellisense popup will still show it as an optional, but analysis will treat it as if it exists.
--!strict
local humanoid = workspace:FindFirstChildWhichIsA("Humanoid")
-- typing out 'humanoid' will popup 'Humanoid?' but will not warn you
print(humanoid.Name)
Interestingly, this also breaks:
-- this used to warn you along the lines of
-- 'Model/Humanoid? could not be converted to Humanoid?/Model'...
-- Even when casting this as : Humanoid, it will still accept it
-- when previously it did not and will warn you.
local humanoid: Model = workspace:FindFirstChildWhichIsA("Humanoid")
I was under the impression this was done on purpose to avoid clashing with these method’s magic types, although I’m all in favor of making their returns optional.
In my mind, static indexing should be typed as if the object is always going to exist while FindFirstChild
and related should assume it may not (because for static indexing, the alternative is a runtime error).
I think these are more so issues with the Luau type solver than the types merely being set incorrectly.
Are you sure that :WaitForChild is also broken?
:WaitForChild is supposed to return Instance unless a timeout is given.
If a timeout is given then indeed the return type should be Instance?
This seems to be working correctly at least for me:
I can replicate the issue related to FindFirstChild
type being Instance instead of Instance? though.
I have amended my comment, thanks for pointing it out.
That’s weird. Also, is this in the right category? Based on my understanding it should be in #bug-reports:studio-bugs , tho I might be wrong
I read somewhere that when you specify the type of a variable, Roblox does not enforce it. So you can still put what you want there. It’s useful when going back to code that you haven’t seen in awhile to make it easier to figure out what you did. It’s also useful for the LUA compiler as it provides type hints to successfully compile the LUA script into machine code.