I’m trying to use type checking to make object creation easier to manage by autocompletion.
In this example code, the last character is highlighted with an orange underline, throwing a warning saying Type 'false' could not be converted into 'boolean' in an invariant context.
--!strict
local mt = {}
mt.__index = mt
function mt:Jump()
self.Moving = true
end
local character = setmetatable({Moving = false}, mt)
if character.Moving then
return
end
character:Jump()
Expected behavior
The type solver shouldn’t complain, as this code would work just fine at runtime, and the Jump method should turn self.Moving into true.
Thanks for the report! This is a known issue we’re already tracking with refinements on table properties in the New Type Solver. The general cause is that the condition character.Moving refines both the read and write types of Moving, meaning that character ends up with a type that does not match the self type in your method. You can work around it by writing the following today, i.e. binding the condition to a local to stop the refinement from taking place:
local isMoving = character.Moving
if isMoving then
return
end
character:Jump()
We have an engineer currently working on fixing this bug in refinements though, so it should hopefully be resolved soon as well.