[Typed Luau] Metatable __index type not being capable of supporting intersections

This post has two parts, first is clarifying the title and the other is the specific implementation I am attempting to use it in, mainly showing that in some obscure cases, it is, to my current knowledge, necessary to type the __index metamethod rather than just intersecting many types.

  • The second part was removed since it is quite lengthy and difficult to parse visually.

PART 1

The autocomplete for my table isn’t working as I expected it to.

type AppleColor = {Color: {R: number, G: number, B: number}}
type AppleSize = {Size: {X: number, Y: number}}
local Apple = setmetatable({}, {
	__index = {} :: (AppleColor & AppleSize)
})

Expected equivalent


Reality

Autocomplete for just one

I do not have any plugins enabled. I tried the new type solver and old one.
I cannot just set the overall type, since this is the simplified version and the actual one involves generics where it is necessary to intersect the types. To my knowledge there is no other approach than using the __index metamethod type to have autocomplete act the way I need for my specific situation.

1 Like

This seems like a bug to me. You can report it via Issues on the Luau GitHub repository:

3 Likes

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