I’m trying to put together a package of utility functions, object constructors etc and I’m trying to access exported types from the loading modulescript, however, they only seem to be accessible via a local reference, and if placed in a table they get “lost”.
--[[Modulescript]]
export type myType = string
return {}
--[[Loading modulescript]]
--This works
local module = require(script.myModule)
local a : module.myType --"string"
--But this does not
local modules = {}
modules.module = require(myModule)
local a : modules.module.myType -- "unknown type modules.module"
I could re-export them by redefining the type in the requiring module and exporting it as a type of that module, which works, but this seems silly.
Is there any other way around it?
(Edit: For clarification, the type info for the objects created this way does get passed on)
I don’t know of any work around but it’s probably caused by the fact that the “modules” module simply stores the value that the module returns, not all the data (such as types). So my guess is this is either a bug or intentional for the reason I mentioned above.
You could choose to only store the module and then when accessing them, you require them.
Thanks for the idea!
However, storing the module doesn’t work unfortunately as require() needs a path which resolves to a modulescript (not a table reference) for the type checker to work.
Just putting them in a folder and requiring them individually also works, but sort of misses what I was aiming for, as I was hoping to be able to require 1 util module, rather than everything individually.
Yeah sadly I don’t know how or if it would be possible to get the types from the modules if they’re stored in a table like that. You’d probably have to move the type exports to the top module.
The one solution I can think about is making a module called “types” and just store all the types in that one, you’ll still have to require the util module and the types but it’s better than requiring a bunch of modules I guess
It’s kind of annoying there isn’t a global export setting for types so you don’t have to redefine them in a types module.
I tried it out, and I think this could work for me, by using a “types” and “utility” module in tandem.