Module Scripts that extra Type Annotation?

After a lot of fighting with bugs and using this guys module LuauParser ( full v0.710 ) - Resources / Community Resources - Developer Forum | Roblox

plugin now has a setting to create this

export type req_Test = {
	Test1: string,
	Test10: { [number]: number },
	Test11: { [string]: string },
	Test12: { [number]: string },
	Test13: { Names: { [number]: string }, Scores: { [number]: number }, Enabled: boolean },
	Test14: { GroupA: { Items: { [number]: number }, Visible: boolean }, GroupB: { Items: { [number]: number }, Visible: boolean } },
	Test15: { Name: string, Nickname: string?, Age: number },
	Test16: { Value: string | number, Status: Status },
	Test17: (callback: (value: number) -> string, value: number) -> string,
	Test18: (name: string, age: number, active: boolean) -> (string, number, boolean),
	Test19: (...string) -> { [number]: string },
	Test2: () -> (),
	Test20: <T>(value: T) -> T,
	Test21: Result<User>,
	Test22: { [number]: User },
	Test23: { Manager: { Handlers: { OnStart: () -> (), OnStop: (reason: string) -> boolean, OnUpdate: (deltaTime: number, callback: (number, string) -> ()) -> () } } },
	Test25: { Name: string, Level: number },
	Test26: (name: string) -> User,
	Test27: {  },
	Test28: { [number]: boolean | number | string | { Name: string } },
	Test29: { Name: string, Children: { [number]: { Name: string, Children: {  } } | { Name: string, Children: { [number]: { Name: string, Children: {  } } } } } },
	Test3: (string: string) -> (),
	Test30: { __index: (self: any, key: string) -> any, __tostring: (self: any) -> string, __call: (self: any, value: number) -> number },
	Test4: { [number]: string },
	Test5: { SomethingElse: { Test5: (string: string) -> any } },
	Test6: number,
	Test7: boolean,
	Test8: boolean,
	Test9: nil,
}

based on this


type Status = "Idle" | "Running" | "Finished" | "Failed"

type Vector2Like = {
	X: number,
	Y: number,
}

type User = {
	Id: number,
	Name: string,
	Nickname: string?,
	Active: boolean,
	Status: Status,
	Position: Vector2Like,
	Tags: {string},
}

type Result<T> = {
	Success: boolean,
	Data: T?,
	Error: string?,
}

return {
	Test1 = "Something",

	Test2 = function()
		print("Hello world")
	end,

	Test3 = function(
		string: string
	)
		print(string)
	end,

	Test4 = {"HI", "SOMETHING", "HI", "SOMETHING"},

	Test5 = {
		SomethingElse = {
			Test5 = function(
				string: string
			): any
				return string
			end,
		}
	},

	Test6 = 12345,
	Test7 = true,
	Test8 = false,
	Test9 = nil,
	
	Test10 = {1, 2, 3, 4, 5} ,

	Test11 = {
		First = "One",
		Second = "Two",
		Third = "Three",
	} :: {[string]: string},

	Test12 = {
		[1] = "One",
		[2] = "Two",
		[3] = "Three",
	} :: {[number]: string},

	Test13 = {
		Names = {"Alice", "Bob", "Charlie"},
		Scores = {100, 85, 92},
		Enabled = true,
	},

	Test14 = {
		GroupA = {
			Items = {1, 2, 3},
			Visible = true,
		},
		GroupB = {
			Items = {4, 5, 6},
			Visible = false,
		},
	},

	Test15 = {
		Name = "Example",
		Nickname = nil :: string?,
		Age = 16,
	},

	Test16 = {
		Value = "Hello" :: string | number,
		Status = "Running" :: Status,
	},

	Test17 = function(
		callback: (value: number) -> string,
		value: number
	): string
		return callback(value)
	end,

	Test18 = function(
		name: string,
		age: number,
		active: boolean
	): (string, number, boolean)
		return name, age, active
	end,

	Test19 = function(
		...: string
	): {string}
		return {...}
	end,

	Test20 = function<T>(
		value: T
	): T
		return value
	end,

	Test21 = {
		Success = true,
		Data = {
			Id = 1,
			Name = "Alice",
		},
		Error = nil :: string?,
	} :: Result<User>,

	Test22 = {
		{
			Id = 1,
			Name = "Alice",
			Nickname = nil :: string?,
			Active = true,
			Status = "Idle" :: Status,
			Position = {X = 0, Y = 0},
			Tags = {"Admin", "Developer"},
		},
		{
			Id = 2,
			Name = "Bob",
			Nickname = "Bobby",
			Active = false,
			Status = "Finished" :: Status,
			Position = {X = 10, Y = 20},
			Tags = {"Tester"},
		},
	} :: {User},

	Test23 = {
		Manager = {
			Handlers = {
				OnStart = function(): ()
					print("Started")
				end,

				OnStop = function(reason: string): boolean
					print(reason)
					return true
				end,

				OnUpdate = function(
					deltaTime: number,
					callback: (number, string) -> ()
				): ()
					callback(deltaTime, "Updated")
				end,
			},
		},
	},
	
	Test25 = ({
		Name = "Example",
		Level = 10,
	} :: {Name: string, Level: number}),

	Test26 = function(
		name: string
	): User
		return {
			Id = 1,
			Name = name,
			Nickname = nil,
			Active = true,
			Status = "Idle",
			Position = {X = 0, Y = 0},
			Tags = {},
		}
	end,

	Test27 = {},
	Test28 = {
		"String",
		123,
		true,
		{Name = "Nested"},
	},
	
	Test29 = {
		Name = "Root",
		Children = {
			{
				Name = "ChildA",
				Children = {},
			},
			{
				Name = "ChildB",
				Children = {
					{Name = "Grandchild", Children = {}},
				},
			},
		},
	},
	
	Test30 = {
		__index = function(self: any, key: string): any
			return rawget(self, key)
		end,

		__tostring = function(self: any): string
			return tostring(self.Name)
		end,

		__call = function(
			self: any,
			value: number
		): number
			return value * 2
		end,
	},
}

there are limitations like children scripts but works good enough for what I need it for.

@vantoanvh thanks for the luau parser

You’re right that there’s nothing off-the-shelf that does this. The fundamental problem is that Luau’s type information only exists at the syntax level, it’s not baked into the runtime or accessible through reflection the way you’d need for a type extractor to work reliably.

What youre hitting is a real architectural issue. The workaround most people land on is breaking the cycle by creating a separate types-only module that holds just the type definitions without any logic or requires. Then both your module and anything that needs its types can require that instead, avoiding the circular dependency. It’s a bit of busywork to set up, but it actually scales, you maintain one thin types file rather than syncing a generated schema every time you touch the implementation.

The other option, if you’re working in a larger codebase, is accepting that you’ll need to manually define the public interface types in a separate place. Not ideal, but it’s honest about the constraint.

2 Likes

It took me a few days after the post, but I was able to make the plugin be able to generate schemas that give the types, so we don’t need to do typeof(require()), with overloads too. I do wish that Roblox made it so we could actually do typeof(require(something)) without causing circular dependency. It’s been something I’ve been facing for a few months, and I’ve heard other larger studios face it too; they handle their stuff by using external tools, though, while I tried making internal tools. The good thing is my plugin can generate the schemas for scripts now. The bad thing is there are limitations based on how people code, but it supports the majority of coding styles which is good enough.