Luau Methods Using Colons Should Be Recognized As Valid

I’ve noticed an issue where Luau doesn’t recognize tables with methods using : as correct when assigning types to the table:

--!strict

type _module = {
	Create: () -> nil,
	Foo: () -> nil,
	__index: _module,
}

local module = {}:: _module
module.__index = module
function module.Create() -- Valid
	local construct = setmetatable({}, module)
	
	construct.Var = "Variable"
	
	return construct
end
function module:Foo() -- Cannot add propery 'Foo' to table '_module'
	print(self.Var) -- "Variable"
end

local obj = module.Create()
obj:Foo() -- Type 'nil' does not have key 'foo'

The warning can be silenced when using . instead of : , although I prefer not doing it this way:

--!strict

type _module = {
	Create: () -> nil,
	Foo: (_module) -> nil,
	__index: _module,
}

local module = {}:: _module
module.__index = module
function module.Create()
	local construct = setmetatable({}, module)
	
	construct.Var = "Variable"
	
	return construct
end
function module.Foo(self) -- Valid, notice how I'm no doing .Foo instead of :Foo
	print(self.Var) -- "Variable"
end

local obj = module.Create()
obj:Foo() -- Type 'nil' does not have key 'foo'

This is very frustrating to deal with as I don’t want to use --!nocheck but --!strict for all my code. Unless I’m doing something wrong I feel like this should be seen as correct.

5 Likes