new luau type solver type error when iterating through a dictionary

So i’ve been recently writing some code until i saw this type error on a perfectly normal working code

Heres the error
qwWwdawdasdwadasd

and heres an example code

local dictionary = {["hi"] = "bad", ["bye"] = "good"}

for something, v in dictionary do
	print(string.match(something, "hi"))
end

putting the dictionary into a pairs function seems to fix the error but it seems a little bit dumb for this error to happen since theres no functional difference between using pairs and no pairs on a dictionary

3 Likes

There’s nothing wrong with this code at all, and the typechecker should probably get the type between something and v anyways. I think the generics behind pairs() (see Generics - Luau) causes the type checker to magically resolve the types between both variables.

(--!strict causes this to work, albeit another error shows up which requires a lot more knowledge on the type system so I won’t include it)

This is lowkey a correct error.

Dictionary has no indexer and Luau can’t refine the types because nothing restricts the dictionary type to anything specific. As a result, the something becomes the generic non-nullable ~nil type, which is effectively “unknown” minus the undefined type (in roblox-ts, we call this a defined type).

Remember, you can pass any type to something that takes an unknown-like, but you cannot pass unknown to something that expects a known type. The latter is which is what string.match tries and does, you cannot pass ‘unknown’ to string.match as unknown can be anything including string.

If you refined something to be something more similar to string, this error would disappear.
This information is actually given when you omit pairs():

RobloxStudioBeta_qcwehvvqrq

In short, because there’s nothing telling Luau what the specific layout of dictionary should be, it assumes it can be indexed by anything and can map to anything. As a result, every key is a non-nullable “anything” and every value is a nullable “anything.”

Aren’t the indexer errors from using --!strict? I don’t think he used --!strict in the original code or example.

The functionality of those directives were also delegated to Workspace properties:
RobloxStudioBeta_5rcf5ORCbe

You don’t need to add them to your code.

wait since when did that option exist
anyways mb

I wouldn’t describe it that way. They do different things. Directives set the mode for that specific script, whereas the option in the scripting category sets the default mode for the whole project.

Thank you for the report! This issue is actually not specific to the new type solver, and has been brought up before in the Luau open source repository, and definitely on the DevForum as well, but search functionality here is a bit tougher. We would really like to fix this, but there are at least a few things that need to happen before that can be possible. In the example you wrote, it’s immediately obvious that dictionary has a fixed collection of elements, and the type we assign to the key and value is clear, e.g. something : "hi" | "bye" and v : "good" | "bad". Unfortunately, it is not obvious in general. Consider this code in a function instead:

function frob(dictionary)
	for key, value in dictionary do
		print(string.match(key, "hi"))
	end
end

Even if we annotate dictionary with a type, e.g.

function frob(dictionary: { hi: "bad", bye: "good" })
	for key, value in dictionary do
		print(string.match(key, "hi"))
	end
end

It is still the case that we could call this function with a dictionary that has more things in it as well, e.g.

frob({hi: "bad", bye: "good", [1]: "foo"})

If we allowed this without error then, any code in the for loop would actually be assigning inaccurate types to key and value. Closing this soundness loophole would require us to instead say that the type of key and value are unknown, which would be accurate, but probably not particularly useful. So, today, our reasonable options are either: a) what we already do, which is give you an error and ask you to write an indexer on the dictionary type, or b) allow iteration, but the type of the fields is unknown and so you’ll still end up having to either cast or write conditionals that refine the type.

In order to do better here, we believe that it’s necessary to differentiate between table types that refer to the exact shape of the table, i.e. describe every field that is present and where the absence of a field in the type guarantees that it will not be present in the table, from the inexact table types that exist today. There’s been a long-standing RFC open for exactly this feature: Exact Table Types. For an exact table type, we could allow iteration and still provide a useful type for the keys and values. This RFC was iceboxed in order to focus on the New Type Solver without growing its scope even more than it already had, but exact table types are something we’re planning to implement in 2026, and you will be able to iterate over them with the expected behavior, even without specifying an indexer.

All that being said, for inexact table types, this is “by-design.” If you’d like to propose that Luau ought to give unknown, unknown instead, I’d say to open that as an RFC or a feature request and we can get various opinions on the two, but otherwise, the intention now is that inexact table types, which are the only kind we have today, will produce this error, and exact table types will not when they ship.

1 Like