Type 'boolean' could not convert to 'number' on a comparison

num < num returns a boolean, but for type checking it throws a warning saying that it could not convert to number, why?

local Operation = math.abs(Number - Number2)
return tonumber(Operation) < 5 :: boolean

image

1 Like
local Operation = math.abs(Number- Number2)
if tonumber(Operation) <5 then return true else return false end```

It’s because you tried to type check the 5 as a boolean instead of the comparison, but you shouldn’t need to because the typechecker already knows that a comparison would return a boolean

1 Like

If you want the function to be type checked you would have to do this:

local function doSomething(): boolean
    return 1 == 1
end

Get rid of the tonumber it’s not necessary. It turns the Operation type from number to number?. If you want to keep it you have to first check that tonumber(Operation) is not nil with an if statement.

Also you need brackets around (tonumber(Operation) < 5)

Lua doesn’t require brackets around comparisons

You do when you’re trying to make it a boolean using ::

Yeah but that would be unnecessary, you already pointed out one of the issues

This fixed it, also I know I didn’t need tonumber I just used it to see if it’ll fix it or not.

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