Optional types inside tables behave strangely in new type checker beta

In the new type checker beta, an optional type inside a table will be converted to non-optional if there is an if statement check if it exists. This makes sense, however this will cause the type checker to issue a warning when trying to set it to nil within that if statement. This does not happen for an optional type outside of a table.

This code can be used to replicate:

	type typeA = {Model:Model?}
	local array:typeA = {["Model"] = Instance.new("Model")}
	
	if array.Model then
		array.Model = nil
		--The above line gives a warning "Type 'nil' could not be converted into 'Model'"
	end

    array.Model = nil
	--The above line does not warn
	
	type typeB = Model?
	local model:typeB = Instance.new("Model")
	
	if model then
		model = nil
		--The above line does not warn
	end
1 Like

Hi there, thanks for the report! The underlying issue here is one that we’ve seen a fair number of times: it’s ultimately a consequence of how refinements work in the New Type Solver. Essentially, when you store something in an array and then write a conditional against it, the refinement changes both the type you’re allowed to read from that field (expected, it’s the reason we have refinements and then thing that allows IsA and typeof and so forth to all work well with the type system), and the type you’re allowed to write to it, which is the source of the bug and is undesirable. One of the folks on our team is actually currently working on this problem, and we hope to have a solution in the near future!

1 Like

Circling back to say that this issue has been solved since then. The given program does not error in either case today. Thanks again for the report, and feel free to open any new issues if you encounter further bugs with refinements!

1 Like

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