Trouble with type checking

Before using luau’s type checking, it was hard to sometimes work with big objects that had a bunch of information because the intellisense didn’t always know what type it was supposed to be. When I discovered type annotation it became a lot easier to write code.

But it’s gotten to a point where I have multiple module scripts and I want to share types to both but it would cause a circular dependency. I’m not sure if it’s best to combine modules or have a separate module that stores types. Both modules are classes. One of them is to make a spaceship object while the other creates a “Path” object. The path object basically stores methods and info on a path that is being used to generate the spaceship.

I’m confused about if there is also a better way to go about this. It feels extreme to have another module to store types which I could just keep in my modules that have those objects. But I also do not want to combine modules as I’m afraid it will get too large and harder to maintain.

ReplicatedStorage
    Types
      SystemA
      SystemB
    Systems
      SystemA --// local types = require(ReplicatedStorage.Types.SystemA)
      SystemB --// local types = require(ReplicatedStorage.Types.SystemB)

Or one big module for every exported time

Is having a module for types a common thing most seasoned devs do? It doesn’t feel right to have to not just write out all the attributes/properties, but also all the methods. I usually export a type (inside the class) that incorporates a metatable type to the class so I can dynamically have those methods already typed for me. But having a type module, I can no longer do that.

export type object = typeof(setmetatable({} :: {attributes}, Object))

There is no way to cleanly do this because that type alias is dependent on an inferred type that is only present inside the module. You cannot hoist it without having to rewrite the method signatures by hand.

This is one of the reasons why I ditched metatables for modeling objects. They have terrible type support and require constant boilerplate to work. Just represent your object as a plain-old-data table and treat each module as a library for any table type that fits the shape of your interface. You even get composition via table subtyping for free!

-- you could easily move these types to a general types module
export type BaseShip = {
	engine: Engine,
	fuel: number
}

export type SpaceShip = BaseShip & {
	rockets: {Rocket}
}

local Ship = {}

-- will work for both BaseShip and SpaceShip
-- or any table that fits BaseShip's interface
function Ship.start(self: BaseShip)
	self.fuel -= 100
end

return Ship
local myShip = {
	engine = createEngine(),
	fuel = 500,
	rockets = {createRocket(), createRocket()}
}

Ship.start(myShip)

It’s completely fine, same way it’s done in webdev with types

So it’s best to use type modules and write the object out without metatables? I see how this works but I’m just a little on edge because it somehow feels wrong. Are you saying to write out both the attributes and the methods? If I add one method to the class, I’d have to keep updating the type. But usually if I just added the properties to the object I wouldn’t have to update it again.

I get how this would seem fine for the module or the server script that is using it, but I have another module script that is using my main module’s object. But since they both need to require each other, I only required one and then blindly written out the rest of the code on that module with no help from the intellisense which took a lot more time than it couldve.

One of them only requires the other for their type, mainly for intellisense. The other requires it for a simple method call (and is also in there for constructing part of a type object). I’m wondering if this is the fault of the coder as maybe I should’ve structured it so that they didn’t need to require each other. I can now see that I might be able to run the method on possibly my server script and not my main module, which possibly gets rid of the circular dependency.

Through my years of luau, I’ve never come across such a horrible problem which would trip me up for months and months on end. Type checking isn’t all that bad but when it comes to stuff like this, it’s by far one of the worst things I’ve had to deal with.

-- Ship module
local pathData = require(blah blah blah) -- circular dependency

local Ship = {}

export type Ship = {
     attribute1: number
     attribute2: number
     Path: pathData.Path -- notice how my ship type actually has the Path type in it, this is related to my actual script.
}

function Ship.new(argument1, argument2): Ship
     local self = setmetatable({},Ship)

     self.attribute1 = argument1
     self.attribute2 = argument2

     return self
end

function Ship.start(self: Ship)
     -- blah blah some other important code...

     pathData.GeneratePath(self)
end
-- Path module
local shipModule = require(blah blah) -- circular dependency

local PathData = {}

export type Path = ...

function Path.new(...): ...
     --- constructer stuff
end

function Path.GeneratePath(self: shipModule.Ship)
-- generates a part of the ship

-- inserts a path object into the ship object (refer to the Ship type to see how they are related)
end

Would it just be better to make it so that both don’t depend on each other? Or would I still just make a big type module with all the types. I’m fine with having attributes typed but still not methods. Though, if there is no other better way then I guess we can go with it.

Hopefully this makes sense, I’ve had this on my mind for far too long.

What I meant was that you only need to write out the table type with just the attributes of the table. All of the ‘methods’ of the object become static functions that you call through the module. This way, if you add another function to the module, you don’t have to update the type, and you are also free to put your types anywhere like in a separate ‘Types’ module.

Ideally, I would say you shouldn’t have any circular dependencies like what you have right now because it is impossible for static analysis to figure it out (as in it is logically proven impossible).

What I would urge you to do instead is move GeneratePath from the Path module to the Ship module. This is more cohesive (stuff in the Ship module does stuff on ships) and should resolve the circular dependency as Path doesn’t have to require Ship, only Ship has to require Path.

(You also don’t need to use setmetatable with this approach, self can just be a regular table since you aren’t attaching any methods to it.)