Loop continue/break on conditional with check does not adjust type

function foo1(bar: number?)
	if not bar then
		return
	end
	bar += 3
end

function foo2(bar: number?)
	for i=1,3 do
		if not bar then
			continue
		end
		bar += 3
	end
end

function foo3(bar: Instance)
	for i=1,3 do
		if not bar:IsA("Model") then
			continue
		end
		bar.PrimaryPart = nil
	end
end

image
image

My examples are quite week, but there are numerous situations where this occurs. Current workaround is a cast or assert.

Expected behavior

Should pickup that the type in that branch of the code is different.

This was also reported earlier in Luau typechecking does not carry type guards after return statements

This is still something that we are looking to fix.
We actually received a solution from the community Support Control Flow type Refinements for "break" and "continue" statements by AmberGraceSoftware · Pull Request #1004 · Roblox/luau · GitHub and I hope that it will get integrated soon.

1 Like

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