Reproduction Steps
When using something like a guard clause in a function, type guards do not propagate to the rest of the function.
Use a variable with two or more possible types, check for its type, and return, break, or continue if it’s not one of those types. The rest of the scope will not have the correct types applied to the variable.
Expected Behavior
This method of type checking should propagate to the scope outside of the return
function optionalArgWithGuardClause(arg: number?)
if not arg then return end
for i=1, arg do --this should work! arg should be of type "number"
end
end
And here’s an example with a break/continue guard:
function optionalArgWithGuardClause(arg: number?)
for i=1, 5 do
if not arg then continue end
if not arg then break end
print(arg) --arg should be of type "number" here
end
end
Actual Behavior
The correct type does not get applied after the guard clause.
function optionalArgWithGuardClause(arg: number?)
if not arg then return end
for i=1, arg do --type error: arg is "number?"
end
end
Again, here’s the example with the break/continue guard:
function optionalArgWithGuardClause(arg: number?)
for i=1, 5 do
if not arg then continue end
if not arg then break end
print(arg) --type error: arg is "number?"
end
end
Issue Area: Studio
Issue Type: Other
Impact: Moderate
Frequency: Sometimes
Date First Experienced: 2022-11-22 00:11:00 (-05:00)