Luau Type Checking - Nil-able values and metatables

I’m scratching my head at a few problems with my typechecking.
I have non-strict on by default.

1. Type ‘boolean?’ could not be converted into boolean.

This is an example. My code doesn’t do this, however, it is a recursive function.

local foo = {}

local quux = {}

foo.bar = setmetatable( {
	baz = function( bool: boolean? )
		
		if ( not bool ) then
			foo.bar.baz( true )
		end
	end,
}, {
  __index = quux
} )

Although this looks fine, luau type checking turns the entire function into a warning with no reasonable answer why.
(The same warnings exist without __index aswell.)

2. Metatable? Type?

I’m currently trying to create an “array” type that might have a metatable, however I searched and I have no clue how to do so.

type array = { any }

This seems to create a warning whenever I try to use getmetatable on it.


This is a demonstration, not code used in my experience.

Even if I try to use typeof, it really doesn’t enjoy that either.

type array = typeof( setmetatable( {}, {} ) )

image

Thank you.

I’m not sure about the first problem.


This would infer that array is an array of any. Using getmetatable, you wouldn’t be getting a metatable type, rather, an array (or table) type.


I’m not sure about this one, but, I suspect that when creating a metatable it’s inferred as a dictionary. You can’t table.find a dictionary.

I would assume using typecasts (::) would fix this problem:

type array = typeof(setmetatable({}:: {any}, {}))

The typecast will assume an array of any type.

I don’t have studio with me right now so I cannot test this.

1 Like

Unfortunately, it seems to still be having the same warning, with an additional one if I try to create the array normally.


I’m somewhat leaning towards the first problem being a luau issue and the second one being a lack of a luau feature. (An @​metatable type would be very nice.)

Thank you,

Array isn’t a metatable yet, set the metatable (comparing a metatable to a non-metatable).

1 Like

image
I’m a little bit brainless, but in the end it still doesn’t get rid of the pesky error.
I’ll just use --!nocheck for now.

You forgot to put the the typecast (::) next to the array in the setmetatable.

I’m not sure what’s causing the warning but I’ll try to get back to you if I figure it out when I have studio with me.

1 Like