Typed Luau: how to solve this?

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

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


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


Then the function fun would receive a string.


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.


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)

local Thing = {}

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