Any way to get all exported types from a ModuleScript?

Curious if anyone knows of a way to get all of the export type's defined in a module script when you require it.

For example, I have a ModuleScript with:

export type TestType = {}
export type TestType2 = {}

return {}

I’d like to know if there’s a way to iterate over all exported types when I require that ModuleScript.

2 Likes

There is not. Maybe try using a table?

Why do you need to iterate through types? The types are used only to define what kind of variable you are creating/expecting to get. They don’t really store a purpose outside of that

thats not how types work…
you do this

local spookyModule = {}
function spookyModule.kill()

end
export type spookyModule = {
   kill = function
}
return spookyModule

Maybe try using a table?

Not sure what you mean by this.

I’m guessing the answer is just no to my original question, at least that’s what I’m getting so far. The other two “answers” were pretty useless to me. Thanks anyway lol.

Because I have a use case where it makes sense? I can think of many reasons for why I’d want to be able to get all of the different types defined in a module, especially if I wanted to take say, hundreds of module scripts, and pull all of the types in all of those module scripts into a single table for easy referencing.

This is not only irrelevant to my question, it’s also wrong. The example that I gave is perfectly valid.

1 Like

Short answer, no. Long answer, not without doing it manually. Luau’s type system is so barebones that we can’t even import all types from a module without reserving the first 30 lines to “export type name = module.name”, kinda defeating the purpose of external type definitions in decreasing code bloat.

The one above who said “return a table” probably meant you should return an array listing all types your module has. Maybe give it a similar structure to this until we finally get a functional system:

export type name1 = ...
export type name2 = ...
export type name3 = ...

return {
    _types = {"name1", "name2", "name3"}
}

Found it out.

Its kind of hacky, but it works.

local module = {}
module.thing = nil
export type thing = {
   ezample: string
}
return module
--main
local types = require(module)
local thing: types.thing = {ezample = "hi"}
thing.ezample = 0 --error

Roblox is the best so ofcourse they will release something like this in the earliest builds.

2 Likes

I know you marked this as solution but its important to note that they will probably invent a proper way to do this so dont use this for long. unless they dont, haha ha.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.