How to get the exported type of a module from each other

2 modules needs to get a exported type from each other, how do I get each other’s exported type without running into the issue of a require loop.

In this example I am using composition

The Car object has a component of an Engine:

local Engine = require(script.Engine)
type self = {
	singletonCreated: boolean,
	obj: any,
	Engine: Engine.Engine,
}

type CarImpl = {
	__index: CarImpl,
	new:() -> Car,
}
export type Car = typeof(setmetatable({} :: self, {} :: CarImpl))

local Car = {}
Car.__index = Car

function Car.new()
   local self  = {} :: self
   self.Engine = Engine.new(self)
   return setmetatable(self, Car)
end

Engine needs to require the Car to receive the exported Car type, however this results in a require loop.

local Car= require(script.Car)
type self = {
	singletonCreated: boolean,
	obj: any,
	Parent: Car.Car,
}

type EngineImpl = {
	__index: EngineImpl,
	new:(parent : Car) -> Engine,
}
export type Engine= typeof(setmetatable({} :: self, {} :: EngineImpl))

local Engine = {} :: EngineImpl
Engine.__index = Engine

function Engine.new(parent)
   local self  = {} :: self
   self.Engine = Engine.new(self)
   return setmetatable(self, Engine)
end

to access exported types you do

module.type

so;

local engine = Engine.new() :: Engine.Engine

or

local function blah(blah: Car.Car)
...

Yes, however i’m looking how to do so in the specific case of when 2 modules need to access each other’s exported types.

To get an exported type you first need to require the module. But when the 2 modules reference each other it causes a require loop.

Found similar issue here, but hasn’t been solved yet: Problem with OOP type checking and cyclic module dependency - #3 by 7z99

just have all the types in a seperate module

1 Like

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