luau icon indicating copy to clipboard operation
luau copied to clipboard

Type 'number?' could not be converted into 'number' on -= and +=

Open HarmosCreations opened this issue 10 months ago • 1 comments

local function findSmallestNumber<K>(t:{[K]:number}):(number,K)
	
	local smallest = nil
	local smallestK = nil 
	
	for k, v in t do
		
		if (not smallest) then smallest = v; smallestK = k; continue end
		if (v > smallest) then continue end
		
		smallest = v
		smallestK = k
		
	end
	
	if (not smallest or not smallestK) then print(t); error("Invalid table to find smallest Number!") end
	
	return smallest, smallestK
end

local t = {["a"]=2} :: {[string]:number}
local v, k = findSmallestNumber(t)

t[k] -= 1 -- Type 'number?' could not be converted into 'number'; type number?[1] (nil) is not a subtype of number (number)

this is a minor issue since i can just t[k] = t[k]-1 but still why not fix the issue itself.

this issue is on all -=, +=, *= ...

HarmosCreations avatar Feb 13 '25 17:02 HarmosCreations