luau
luau copied to clipboard
Type 'ConnectionObject?' could not be converted into 'ConnectionObject?'
Reproduction code
--!strict
type Function = (...any) -> (...any)
type ConnectionObject = {
Disconnect: () -> ()
}
type SignalObject = {
Connect: (self: SignalObject, f: Function) -> (ConnectionObject?),
}
type SignalInterface = {
Signal: SignalObject
}
function Once(self: SignalInterface)
local Connection; Connection = self.Signal:Connect(function(... : any)
if Connection and typeof(Connection["Disconnect"]) == "function" then
Connection:Disconnect()
end
end)
return
end

Can be reproduced on
- Roblox Studio 0.574.0.5740446
- Luau 0.571 (using
luau-analyze) (MSBuild VS2022) - Luau 0.574 (using
luau-analyze) (MSBuild VS2022)
This happens when error was generated at a point where type wasn't known to be ConnectionObject?.
I would suggest adding a type annotation to avoid the error:
type Function = (...any) -> (...any)
type ConnectionObject = {
Disconnect: (self: ConnectionObject) -> ()
}
type SignalObject = {
Connect: (self: SignalObject, f: Function) -> (ConnectionObject?),
}
type SignalInterface = {
Signal: SignalObject
}
function Once(self: SignalInterface)
local Connection: ConnectionObject?; Connection = self.Signal:Connect(function(... : any)
if Connection and typeof(Connection["Disconnect"]) == "function" then
Connection:Disconnect()
end
end)
return
end
I would suggest adding a type annotation to avoid the error:
From my understanding, wouldn't the SignalObject's Connect: (self: SignalObject, f: Function) -> (ConnectionObject?) already handle this? (as in the return being a ConnectionObject?)