Ask some questions about the type checker

How do I tell the type checker that the method in the table is a method (using Table:Function()) instead of a normal function (using Table.Function())

Some codes have :: What does this mean?

:: is to set the type of a value when you’re not making a local variable.

local module = {}
module.Item = {} :: {Coins: number}
module.CurrentMap = nil :: Model?
module.Cameras: {Model} = nil -- Error here
1 Like

The colon syntax for table:function() is just that, syntax. For a function definition it means that self is the first argument. You can type it explicitly by using the dot syntax instead (or casting it inside). Keep in mind that the type checker is purely for linting, it will have no effect on execution. For function calls the colon means the table itself is passed as the first argument.

Update: The colon syntax doesn’t actually infer the type of the table as self as I originally thought. Updating this with correct information. @wsyong11


local tbl = {}

function any)
	-- self is explicitly the first argument
	-- self is explicitly the type "any"

function tbl:bar()
	-- self is implicitly the first argument
	-- type of self will be inferred from usage inside this scope
	-- e.g. self.anything will not autocomplete until it has been used once inside this function

-- These are equivalent (same thing for bar() )

Double colon is casting the value on the left to the type on the right. You can use it to e.g. to silence type warnings. You can also use single colon to set the type when defining a variable.


local var: number = nil -- var is of type number

local function foo(): number -- function returns number
	return var

local function bar(): string -- function return string
	return var :: any -- cast to any to fulfil expected type
1 Like

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