Sometimes methods inherited via __index aren't recognized when using type checking typeof() method

When trying to define a type using the type checker’s typeof() method, methods inherited through __index that assert self’s type as the type returned from typeof(), aren’t recognized.

Where it happens: Happens in Studio only

When it happens: I first noticed it August 16 2022 at 11:30 PM AST

Reproduction instructions:
This should reproduce the issue 100% of the time.

This happens in both nonstrict and strict modes, however only in strict does it emit a warning.

To reproduce, write the following in some LuaSourceContainer:

--!strict

local module = {}
module.__index = module

function module:method()
	self = self :: ModuleType
end

function module.new()
	return setmetatable({}, module)
end

export type ModuleType = typeof(module.new())

local m = module.new()
m:method()

Now, a warning will appear at m:method() telling you type “ModuleType” does not have key “method” despite inheriting the method through the __index metamethod. Lua does not error either.

This warning also disappears once you remove the self = self :: ModuleType line from the module:method() function.

The method does appear in the auto complete.

We’ve filled a ticket into our internal database for this issue, and we will update you when we have further information.

Thanks for flagging!

2 Likes