What is type `{}`?

What is type {}? I didn’t know it’s possible just to leave it like this. I thought in the beginning that it’s the same thing as {[any] any}, but solver still tells me that it’s another type. What is it?

1 Like

What do you mean? Could you give me an example?

Maybe it’s just a literal for {}? I don’t have any other ideas. And I don’t know why I would need it ever in my code.

Yes it is. That’s right. That just means table.

But the problem is that it’s not “just a table”. I am not able to pass it into table.clear for example:

How about just setting it to any? Or do you not want that?

I was just interested in knowing what does this exact type mean. I know that I can use both {any} or {[any]: any} if I really want

{any} means the table is array, and its all indexes can be any types. On the other side, {[any]: any} means the table is dictionary, and its keys and values can be any types.

I understand. I mean what is type {}? This is the question. I am able to give this type to a variable.

It’s not just table since I am not able to pass variable of this type to table.clear

That’s because _rawcount isn’t table.

Nevermind. {} type means an empty table.

1 Like

But why then I am able to write this?

So, to be precise, it’s not an empty table, but rather a table whose contents are not guaranteed. The table.clear() function throws a type error because it can’t infer what’s inside the table, but the code above doesn’t throw a type error because it’s a table.

But a table whose contents are not guaranteed is {[unknown]: unknown}, isn’t it?

And we are also able to use type above in table.clear:

{} is simply the supertype of all tables. The code you mentioned has a similar behavior, but it’s not exactly the same.

It’s more of advanced typing

Say we have a class setup like this:

--!strict
local myClass = {}
myClass.__index = myClass

type class<K, V> = {
	k : K,
	v : V,
	
	SomeMethod : (class<K, V>, someVariable : string) -> V
}

function myClass.new<K, V>(k : K, v : V) : class<K, V>
	local self = setmetatable({}, myClass)
	self.k = k
	self.v = v
	return self :: any
end

function myClass.SomeMethod<K, V>(self : class<K, V>, someVariable : number) : V
	return tostring(self.v) .. tostring(someVariable)
end

return myClass

Studio intellisense would recognise what type the k, and v arguments are when passed through .new

So for example:

myClass.new(5, 'Hello')

This would make K : number and V : string
Which means when you run

myClass.SomeMethod(self, 5)

The returned type would be inferred as a string since V is a string.

you can read more in depth on this some where else but this is the basic gist of it

1 Like

However, in the case of table.clear(), the type error will disappear if you do it like the code you mentioned; {} is the supertable of all tables, but there may be conflicts during the inference process.

If you want to get rid of the type error just do

table.clear(self._rawCount :: any)

and that should clear the type error

thanks you for explaining all of this, but at the same time, with all my respect, it’s not related to the question. i know what is generics and question in the topic is only about specific type {}. Not a type {number}, not a {[K] : V} or anything like that. just type {}.