2

I'm currently making a strict type checked script but can't find a way to type annotate a variable that requires a Module Script without using the type any which is bad practice.

DoSomethingScript:

--!strict

local moduleScript: any = require(script.SomethingModule) -- How do I change the any type here?

moduleScript:DoSomething()
moduleScript:NotMethodAndShouldRaiseWarning() -- Using the any type doesn't give warnings for non-existant methods

SomethingModule:

local SomethingModule = {}

function SomethingModule.DoSomething()
    print("Doing something")
end

return SomethingModule

I tried looking at the Roblox Creator Hub documentation and Luau website for how to type annotate a Module Script but can't find anything about it.

So how would I define the type for a Module Script?

2 Answers 2

1

You are looking for the export type keywords.

The Luau documentation has a section for 'Module Interactions' and you can pair that with the section for 'Adding types for faux object oriented programs' which you can use to describe your ModuleScript's returned table.

For example, in your ModuleScript :

-- define the shape of the ModuleScript's returned table
export type MathUtilImpl = {
    add : (a: number, b: number)-> number,
    subtract : (a: number, b: number) -> number,
}
local MathUtil : MathUtilImpl = {}

function MathUtil.add(a, b)
    return a + b
end

function MathUtil.subtract(a, b)
    return a - b
end 

return MathUtil

I'm not using --!strict in the ModuleScript because I didn't want to define everything on the local MathUtil line. But the documentation has suggestions on how to do it.

Including your ModuleScript will bring the exported types with it. Then, Roblox's linter will give warnings if you are using something incorrectly.

Then in your Script :

--!strict
local MathUtil = require(script.ModuleScript)
print(MathUtil.add(1, 2)) -- returns 3
print(MathUtil.subtract("foo", "bar")) -- gives errors 'Type "string" could not be converted to number'
print(MathUtil.multiply(3, 4)) -- gives error 'Key "multiply" not found on table "MathUtil"'
Sign up to request clarification or add additional context in comments.

Comments

1

Ideally, you should not have to type the variable for a modulescript at all. Luau can infer the type of the modulescript automatically if it isn't defined at runtime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.