Typechecking typeof() method has problems casting a type to a table's entry when the entry to the table is not explicitly given

Description and repro instructions

If, for example, you try to inject an object such as a table into a module, the linter has trouble specifying a type for said injected module when you try to type cast using module.index = module.index :: typeof(require(otherModule)), its type gets assigned as ‘any’ and does not show any autocomplete despite a type being casted. It should behave identically to when I do module.index = require(otherModule).

local module = {}
module.classlibrary = module.classlibrary :: typeof(require(script.Parent.Parent.classlibrary)) 
--[[  ^module.classlibrary's type is now any any when it should be a table like so:
		{
			__index = m1
			m2 = metatable ( {}, {__index = m1} )
		}
-- if you try to type module.classlibrary, the "autocomplete" box that shows up will say any
-- should you attempt to index module.classlibrary for its members, autocomplete does not show up
]]

local classlibrary = module.classlibrary :: typeof(require(script.Parent.Parent.classlibrary))
-- classlibrary: any
-- if you attempt to index classlibrary, autocomplete does not show up

local classlibrary = require(script.Parent.Parent.classlibrary)
-- now if you attempt to index classlibrary, autocomplete /does/ show up

return module

As you can see, autocomplete does not show up as its type is defined as “any”

And for expected behaviour,

local module = {}
module.classlibrary = require(script.Parent.Parent.classlibrary)
-- now if you index module.classlibrary, autocomplete does show up because you explicitly assigned module.classlibrary
-- if I assign module.classlibrary = module.classlibrary :: typeof(script.Parent.Parent.classlibrary), this is what should happen

return module

Only now does autocomplete does show up as expected:

Repro files
Here’s a repro file with two scenarios (ReplicatedStorage):
ModuleScript named initializer and its child: to reproduce the bug,
ModuleScript named initializer2 and its child: expected behaviour,
A module called “classlibrary” for some random entry to show that autocomplete does not work as expected

typechecking.rbxl (41.5 KB)

We’ve filed a ticket into our internal database for this issue, and will come back as soon as we have updates!

Thanks for the report!

2 Likes

Hi there, and thanks for the report! This issue required us to expand data model awareness for the type system, which was not tractable without major changes to the type inference engine. We’ve spent the past couple years undertaking exactly such an effort, and this problem is fixed in the New Type Solver.

image

1 Like