How to type check a requiring module

Hey there, I haven’t really used typechecking in roblox yet. So I am pretty bad at it. I am working on a script but while requiring my module I am having a hard time defining a type to the variable that requires the module. Here is the code thus far:

--!strict
--// Services
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

--// Modules
local Modules: Folder = RS.Modules
local BigNum = require(Modules:FindFirstChild("BigNum"))
local Formatter = require(Modules:FindFirstChild("Formatter"))

--//Functions
local function IsLessThan(n1: number | string, n2: number | string)
	local Big1 = BigNum.new(n1)
	local Big2 = BigNum.new(n2)

	return BigNum.lt(Big2, Big1)
end

local function SetText(Label: TextLabel, NewVal: string)
	if IsLessThan(999, NewVal) then
		Label.Text = NewVal
	else
		Label.Text = Formatter.Format(tonumber(NewVal), 2)
	end
end

Also, how would I specify the type of a BigNum.new()

1 Like

I believe you can use the Module.Type syntax. Type checking - Luau
So for example in your implementation of arbitrary/multi- precision module, you can use BigNum.BigNum (or BigInt or mpz_t or whatever you call it) for the script that requires the BigNum module.

You can use something like export type BigNum = typeof(BigNum.new(...)) with ... being the arguments to the BigNum.new function which you can put in the BigNum ModuleScript.

For example.
Module Script for BigNum

--[[...]]
function BigNum.new(...)
        --[[...]]
end
--[[...]]
export type BigNum = typeof(BigNum.new(...))
return BigNum

Script that requires BigNum

--[[...]]
local BigNum = require(--[[BigNum ModuleScript]])
--[[...]]
-- Now this local variable is the BigNum type from the BigNum module.
local bignumvalue: BigNum.BigNum = BigNum.new(...)
--[[...]]
1 Like

Thanks I’ll check this all out in a second

Ok well that seems to work great.