How to export nested types?

-- MODULE A
export type MyType = "foo" | bar
-- MODULE B
local ModuleA = require(ModuleA)
...
-- MODULE C
local ModuleB = require(ModuleB)
local myVar: ModuleB.myType = "foo"

I’m aware that we can just require the top-level module, but is there a way for the type to be propagated?

I don’t understand what you are trying to do

Normally, we’d be able to export a type definition of a module, which would allow whatever scripts that require it to use that type as well. For example:

export type MyType = "foo" | "bar"

local module = {}
...
return module

And the requiring script:

local module = require(<path.to.module>)

local var: MyType = "foo"

Ok, that works without issue. Here’s the thing, though: what if the requiring script is a module script itself, and we want scripts that require it to also be able to use the type?

I dont that you can export nested type yet.

When you import the type from module 1 into the module 2, you would also have to export it from module 2 after you import it. There’s no other way as far as I’m aware to propagate it in that manner.
I like to have one module dedicated exclusively to types and then requite it for anything that involves them.

If I understand your question correctly, can’t you just export module A’s type in module B?

-- MODULE B
local ModuleA = require(ModuleA)
export type MyType = ModuleA.MyType
1 Like

The biggest brain.

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