TLDR;
Title: How should I organize my exported types to be maintainable and convenient to access?
Context
Right now, I have a single module script “Types" that holds all my types so that it is convenient to access any type from any script. So if I have a type Action
representing some class and a type ActionMode
representing some enumeration I can simply write code like this to access it:
local Action = require(Path.To.Action)
local Types = require(Path.To.Types)
local actionMode: Types.ActionMode = Types.ActionModes.default
local action: Types.Action = Action.new(actionMode)
This allows me to conveniently distinguish types from objects/classes and also have type-checked enumerations by centralizing everything under the “Types" module script. The reason I chose not to put the Action
type under the Action
module script is that in my code I likely have other types related to Action
(for example’s sake, say ActionMode
) and in order to get those I would have to write:
local Action = require(Path.To.Action)
local actionMode: Action.ActionMode = Action.ActionModes.default
local action: Action.Action = Action.new(actionMode)
Problem
While I think this looks fine for the enum ActionMode
, it feels odd to write Action.Action
. The issue with my current approach of having a single “Types” module script is that all the types are just put together in there, making it unwieldy and messy and I want to refactor before it gets any worse.
Plus if I have other module scripts whose parent is the “Action” module script and they have their own types, I would have to either re-define the exported types in the “Action” module in addition to in the child modules of “Action” if they themselves use said types or require both the “Action” module and the child modules. This is because chain-requiring does not give access to exported types in the sub-modules.
Attempted Solution
Before learning that chain-requiring is not supported by the type-checker, I thought it would be nice to have that single “Types” module which requires children modules that serve to group related types, thereby keeping the system maintainable and still easy-to-access by just having to write require(Path.To.Types)
at the top of each script. But as I said the type-checker does not support this kind of chain requiring.
Anyone have any ideas?