Intellisense should treat the "continue" keyword as a guard clause

return has a really nice functionality where it acts like a validation statement:

--!strict

local mightExist: number?
if mightExist then
    mightExist += 1 --> valid
end
--!strict

local mightExist: number?
if not mightExist then
    return --> guard clause
end
mightExist += 1 --> valid

continue, unfortunately, does not have the feature:

--!strict

for i = 1, 10 do
    local mightExist: number?
    if not mightExist then
        continue --> guard clause
    end

    -->> proceeding line will only run if var "mightExist" is a number
    mightExist += 1 --> Type 'number?' cannot be converted to 'number'
end

The above snippet will always spur out predictable valid code, so it should be treated as such.

11 Likes

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

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

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.

3 Likes

Solved here:

Thank you!

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