Typed Luau: how to solve this?

I’m learning the Luau type system and I don’t know how to solve this
image

u: number?

Possibly a number, Possibly not. At least from what I can assume.
You’re passing a “Possible Number” to a “Definitely Number” function

Remove the ? and you’ll be sorted.

1 Like

But I don’t want to do that. The statement self._number = u or 0 should resolve the abiguity and self._number should be a number (not a number?). At least that is what happens in typescript.

Yes but their is a chance it won’t be a number. Which is why it’s saying you can’t convert a questionable data type into a real datatype.
It comes down to the way the intelligence handles your code. Unfortunately roblox’s code intelligence is kinda bad, which is why I use things like Visual Studio Code.

The engine:

if type(Argument) ==  "number" then

end

^ psuedo code to help you understand, You’re argument isn’t a number. It’s a questionable number.
if you did

Thing:Action("abc")

Then the function fun would receive a string.

2 Likes

this is true and it always disappoints me. Thank you for helping me, now it is clearer to me.

1 Like

I just solved it by doing this. It’s definitely a roblox intellisense problem.
image

3 Likes

You can use the :: type assertion operator if you don’t want to keep those if statements you’ve used to fix it:

function fun(x: number)
	print(x)
end

local Thing = {}

function Thing:Action(u: number?)
	self._number = u or 0
	fun(u :: number)
end

image