Hey there, I have a question and I don’t want to spend too much time figuring it out myself so any help is appreciated.
Let’s say we have a module script that is a parent of the utilities like Spring, Maid, Roact and etc.
The module script looks something like this:
local ModuleCache = {}
--// Variables
local CachedModules = {}
--// This gets called on server startup
function ModuleCache.Init()
--// Require and gather all the modules into a table
for _, ModuleScript in pairs(script:GetDescendants()) do
if ModuleScript:IsA("ModuleScript") then
CachedModules[ModuleScript.Name] = ModuleScript;
end;
end;
end;
--// This only gets called after .Init() has completed
function ModuleCache:RequireModule(Params: string)
local IndexedModule = CachedModules[Params];
if IndexedModule ~= nil then
return require(IndexedModule);
end;
end;
return ModuleCache
Now let’s say we want to get a module with the RequireModule
method from another script:
local ModuleCache = require([ModuleScriptPath])
local Spring = ModuleCache:RequireModule("Spring")
--// It will return the module but it won't be type checked because the script thinks that the CachedModules table is empty as the modules only get added into it after .Init() has been fired.
Spring. --// No autocomplete
Is it possible to make this fully type-checked so it would support autocomplete, if so then how?