Hi I’m new to type checking and want to learn it solely for autocompletion e.g. variables that have been passed around via modules.
As of right now I’m trying to export a type to another module so I can have autocorrect.
I will cut out most of the code as it’s not needed for this post.
PathData Module:
-- Services
local sss = game:GetService("ServerScriptService")
-- Modules
local modules = sss.Modules
local shipModule = require(script.Parent)
local roomInfo = require(script.Parent.RoomInfo)
-- ServerScriptService
local shipAssets = sss.ShipAssets
-- Workspace
local shipFolder = workspace.Ship
type Ship = shipModule.SpaceshipGeneration
local PathData = {}
PathData.__index = PathData
type PathDataInfo = {
Parent: Model;
Current: BasePart;
Corridor: boolean;
Corridors: {string};
Child: Model?;
}
export type PathData = typeof(setmetatable({} :: PathDataInfo, PathData))
function PathData.new(parentRoom: Model, currentExit: BasePart, isParentCorridor: boolean): PathData
local self = {}
self.Parent = parentRoom
self.Current = currentExit
self.Corridor = isParentCorridor
self.Corridors = {}
return setmetatable(self, PathData)
end
function PathData:GenerateRoom(ship: Ship): Model? -- ship type is seen as an "any" type
-- doesn't auto complete any of this:
local info = ship.Info
local currentNum = ship:GetCurrentNum()
-- ...
end
return PathData
Then I have my main module:
-- Services
local sss = game:GetService("ServerScriptService")
local cc = game:GetService("CollectionService")
-- Modules
local modules = sss.Modules
local roomInfo = require(script.RoomInfo)
local rarityModule = require(modules.RarityModule)
local PathData = require(script.PathData)
-- ServerScriptService
local shipAssets = sss.ShipAssets
-- Workspace
local shipFolder = workspace.Ship
-- Global Variables
local rng = Random.new(tick())
-- Types
type PathData = PathData.PathData
local SpaceshipGeneration = {}
SpaceshipGeneration.__index = SpaceshipGeneration
type ShipData = {
Settings: {
OptionalRooms: number;
EventChance: number;
},
Info: {
Paths: {[BasePart]: {PathData}};
CurrentPaths: {BasePart};
RoomsGenerated: {
RequiredRooms: {Model};
OptionalRooms: {Model};
Other: {Model};
};
StartRoom: Model;
EventRoom: Model;
EventRoomPlacement: number;
TotalRoomsNeeded: number;
}
}
export type SpaceshipGeneration = typeof(setmetatable({} :: ShipData, SpaceshipGeneration))
function SpaceshipGeneration.new(OptionalRooms: number, EventChance: number): SpaceshipGeneration -- creates a new ship
local self: SpaceshipGeneration = setmetatable({}, SpaceshipGeneration)
local totalRoomsNeeded = #roomInfo.RoomTemplate.RequiredRooms + OptionalRooms
local startRoom = GetStartRoom(roomInfo.StartRooms)
local eventRoom, roomPlacement = GetEventRoom(roomInfo.RoomTemplate.EventRooms, EventChance, totalRoomsNeeded)
totalRoomsNeeded += (eventRoom and 1 or 0)
self.Settings = {
OptionalRooms = OptionalRooms;
EventChance = EventChance;
}
self.Info = {
Paths = {};
CurrentPaths = {};
RoomsGenerated = {
RequiredRooms = {};
OptionalRooms = {};
Other = {};
};
StartRoom = startRoom;
EventRoom = eventRoom;
EventRoomPlacement = roomPlacement;
TotalRoomsNeeded = totalRoomsNeeded;
}
for _, exit in shipAssets:FindFirstChild(self.Info.StartRoom):GetChildren() do -- gets all the exits in the start room
if exit:IsA("BasePart") and cc:HasTag(exit, "Exit") then
local pathData = PathData.new(startRoom, exit, false)
self.Info.Paths[exit] = pathData
table.insert(self.Info.CurrentPaths, exit)
end
end
return self
end
return SpaceshipGeneration
The main problem right now is that in my “PathData” module I just want to have the ship var have all the properties and functions as its module( So I can use autocomplete and not have to look back and forth for everything). So I did something like this:
export type SpaceshipGeneration = typeof(setmetatable({} :: ShipData, SpaceshipGeneration))
Honestly I don’t even know if I’m using types right. If I’m doing anything wrong please let me know.
Thanks,
MonsterTrident