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

Hey there, and thank you for the report! Happy to report that this issue is resolved by the New Type Solver which expanded our ability to make inferences about metamethods and inferences that are not ordering-dependent in the code.

image

1 Like

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