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