Cannot initialize a module method with Luau Types

Hi! I am currently testing the new Roblox feature which is type-checking.

I am making a module with useful functions to be used by other scripts, but I have had a problem, and that is that I can’t specify a function when the module variable has been created.

This is my code:

-- MODULE
local UtilsFunctions: Typedef = {}
-- METHODS
function UtilsFunctions.GetPlaytimeLevel(playtime: number): string
	return ""
end
function UtilsFunctions.CreatePlayerLeaderstats(playtime: number): ()
	UtilsFunctions.GetPlaytimeLevel(1234) -- I need to use this function so this function can work.
end
-- EXPORTS
export type Typedef = {
	GetPlaytimeLevel: ((playtime: number) -> string),
	CreatePlayerLeaderstats: ((playtime: number) -> ())
}
-- RETURNS
return UtilsFunctions

Error:

You can omit the type definition on the local variable(line 2) into

--MODULE
local UtilsFunctions = {}

and typecast the return line instead:

--RETURNS
return UtilsFunctions::Typedef
1 Like

Hey! Looks like it worked, but now if I remove a function, it doesn’t give me the warning that there is missing 1 function.

Example:
Here I removed the function CreatePlayerLeaderstats, so it should give me a warning because it’s mandatory.

-- MODULE
local UtilsFunctions = {}
-- METHODS
function UtilsFunctions.GetPlaytimeLevel(playtime: number): string
	return ""
end
-- EXPORTS
export type Typedef = {
	GetPlaytimeLevel: ((playtime: number) -> string),
	CreatePlayerLeaderstats: ((playtime: number) -> ())
}
-- RETURNS
return UtilsFunctions :: Typedef
-- EXPORTS
export type Typedef = {
	GetPlaytimeLevel: ((playtime: number) -> string),
	CreatePlayerLeaderstats: ((playtime: number) -> ())
}
-- MODULE
local UtilsFunctions: Typedef = {}
-- METHODS
function UtilsFunctions.GetPlaytimeLevel(playtime: number): string
	return ""
end
-- RETURNS
return UtilsFunctions

It has to be at the first definition of the table for typechecking to work and show the warning you are expecting

1 Like

It still doesn’t work, I don’t receive the warning… But instead, I receive the same warning again.

Anyways, the alternative that comes to mind is to set another variable with the type definition like this:

local foo:Typedef = UtilsFunctions
-- RETURNS
return foo

which is less cleaner but warns against the removal of table values.

1 Like
-- EXPORTS
export type Typedef = {
	GetPlaytimeLevel: ((playtime: number) -> string),
	CreatePlayerLeaderstats: ((playtime: number) -> ())
}
-- MODULE
local UtilsFunctions: Typedef do
	-- METHODS
	UtilsFunctions = {
		GetPlaytimeLevel = function(playtime: number): string
			return ""
		end,
		CreatePlayerLeaderstats = function(playtime: number)
			
		end,
	}
end
-- RETURNS
return UtilsFunctions

It looks sort of ugly but reality is reality so deal with it

3 Likes

Luau has a built-in typeof function, try that maybe?

--!strict

-- MODULE
local UtilsFunctions: Typedef = {}
-- METHODS
function UtilsFunctions.GetPlaytimeLevel(playtime: number): string
	return ""
end
function UtilsFunctions.CreatePlayerLeaderstats(playtime: number): ()
	UtilsFunctions.GetPlaytimeLevel(1234) -- I need to use this function so this function can work.
end
-- EXPORTS
export type Typedef = typeof(UtilsFunctions)
-- RETURNS
return UtilsFunctions

Hey! It worked perfectly! But, could you tell me why you use do - end instead of using the operator = in the variable?

do end makes a new temporary scope that is active until it reaches the end from the do statement, since using = {} is the one causing issue, I’ve went ahead and made a new scope and defined the variable there (which also in return give you working typechecking under the cost of “weird” code structure)

It’s like making a function and then calling it instantly, except this one just makes a scope that runs once following the flow of the code instead of being able to be called from any line

1 Like