luau icon indicating copy to clipboard operation
luau copied to clipboard

Type 'ConnectionObject?' could not be converted into 'ConnectionObject?'

Open MaidenlessUsr opened this issue 2 years ago • 2 comments

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

image

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)

MaidenlessUsr avatar May 05 '23 14:05 MaidenlessUsr

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

vegorov-rbx avatar May 05 '23 15:05 vegorov-rbx

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?)

MaidenlessUsr avatar May 06 '23 04:05 MaidenlessUsr