While checking for a condition in my killstreak class, I cannot then reassign the value within the statement, eg.
Killstreak.End = function(self: Killstreak): boolean
if self._isActive then
self._isActive = false --Type Error: Type 'false' could not be converted to 'true'
end
end
I presume this is an issue related to type inference, but how can it be avoided while still asserting a value within the function to keep the code safe? Using assert() does the same thing.
The _isActive property is annotated as boolean, not true. I think your example works because local Z: X = {Y = true} is an assignment of a table of type X to a variable, also of type X. My problem is that during the condition where self._isActive is stated to be true for all code that runs within the block, the type solver infers the attribute as, correctly, true. This causes the variable to become nearly immutable in the eyes of the type solver, as it can, and can only be, of the value that the expression evaluates to a truthy value for, and so I cannot do anything involving the assertion of a variable before assigning it a new value.
Killstreak.End = function(self: Killstreak): (boolean, string)
if not self._isActive then
return false, "Killstreak is already inactive"
end
self._isActive = false --Moved the assignment so the assert can happen at the start
of the method, the type checker still gives the same warning
self._ended = DateTime.now()
end
With the code you have provided, I am unable to replicate the error. The typing engine has some issues with staying up-to-date, so you may have made made a change somewhere the masks an original conflict. Try restarting Roblox Studio or closing all your scripts with Ctrl + W, then reopening your problem-script. If the error still persists, then weâll have to do more digging.
On another note, your constructor has a type conflict. Write the following instead:
return setmetatable(self, Killstreak) :: any
Itâs also best that you cast self to the Killstreak type before assembling it:
Thanks for pointing out the conflict, I probably should also have mentioned that Iâm using Robloxâs new type solver, which I forgot is still in beta
I had luck quieting the checker by splitting the class and instance into sep types when I ran into something similar. Something like below to experiment with if all else fails.
It still warns when I coerce it, though no with âCould not convert type âbooleanâ to âtrueââ. Iâll try to split the types once I have the time to.