Setting the type to the type that created this type

Hey guys so I have 3 types in my module

export type Constructor = {
	new: () -> Signal,
}

export type Signal = {
	Connect: (self: Signal, func: (...any) -> ()) -> Connection,
	Once: (self: Signal, func: (...any) -> ()) -> Connection,
	Wait: (self: Signal) -> ...any,
	DisconnectAll: (self: Signal) -> (),
	Fire: (self: Signal, ...any) -> (),
}

export type Connection = {
	--Signal: Signal?, -- Adding this breaks it
	Disconnect: (self: Connection) -> (),
}

The Constructor type has a function called new that creates the Signal type and the Signal type has a function called Connect that creates the Connection type

up to now everything work fine but the problem happens when I try to have a property inside the Connection type that is the type Signal it does not seam to like to have a type that it was created by

is there a way to make this work?

what do you mean “breaks it?”

I attempted the same thing and no errors
my only question is do you really need the Signal as a nullable value?
because then the code will scream at you if you try to run anything on it because it “could be nil”

image

when the connection is disconnected the signal property becomes nil

kind of a dumb question, but do you have signalModule returning a constructor type?

IE
return signalModule::Types.Constructor

the module returns the constructor like this

local constructor: Constructor = {}

return table.freeze(constructor)
1 Like

this is actually a really peculiar type definition issue.

I think its an issue with the Constructor class for some reason. I would just let Luau define it regularly

image

image

I have literally no idea why this doesn’t work as it obviously should

1 Like

yup that works for me as well

and also strange why it does not work the other way

iv posted a issue on github here: Can not set type to type that created type · Issue #758 · Roblox/luau · GitHub

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.