Type Annotation in Luau is not consistent (Beta Feature Enabled)

Read the code snippet. I got a type error for the second if, but not for the first if. (I use --!strict)

I got no warning here:

if transparency > 1 then
	warn("transparency > 1")
	transparency = 1
end

But I get a warning here.
Type number and number? cannot be compared with < opperator. :

if transparency < 0 then
	warn("transparency < 0")
	transparency = 0
end

After the first checks, the luau type checker should be smart enough to know that transparency is no longer number?, it is now number. I placed an if with: if not transparency then make it 0, like this:

if not transparency then
	transparency = 0
end
if typeof(transparency) ~= "number" then
	warn("transparency and typeof(transparency) ~= \"number\"")
	return
end

Is this a BUG or it is normal behavior? If it is normal behavior, then type cheking (in my opinion) is unrelaiable and I will stop using --!strict with 100% trust now.

This is thw whole function code:

Shared.DeepSetTransparencyOfInstance = function(instance: Instance, transparency: number?)
	if not instance then
		warn("not instance")
		return	
	end
	if typeof(instance) ~= "Instance" then
		warn("typeof(instance) ~= \"Instance\"")
		return
	end
	if not transparency then
		transparency = 0
	end
	if typeof(transparency) ~= "number" then
		warn("transparency and typeof(transparency) ~= \"number\"")
		return
	end
	if transparency > 1 then
		warn("transparency > 1")
		transparency = 1
	end
	if transparency < 0 then
		warn("transparency < 0")
		transparency = 0
	end
	
	if instance:IsA("BasePart") then
		instance.Transparency = transparency
	end
	
	local descendants = instance:GetDescendants()
	for _, descendant in ipairs(descendants) do
		if not descendant:IsA("BasePart") then
			continue
		end
		descendant.Transparency = transparency
	end
end

BTW: I have enabled the:

2 Likes

welcome to the club

i don’t find strict luau usable in the slightest yet i suffer through it anyway :saluting_face:

1 Like

I am using it im my whole code base, sometimes works as expected and I like having it on because we are humans and an extra check dosn’t hurt (we all do misstakes), but with more complicated code, it gets in the way instead of helping.

Yeah, that’s exactly how I feel.

Types especially. Tagged unions are useful but painful because there’s no sugar for them.

Your problem appears to be a problem with type narrowing. typeof(transparency) ~= "number" should ensure everything after it treats it like a number since it returns early but the type solver hates us so nope!

Anyway this change seems to fix it:

--!strict
Shared.DeepSetTransparencyOfInstance = function(instance: Instance, transparency: number?)
	if not instance then
		warn("not instance")
		return	
	end
	if typeof(instance) ~= "Instance" then
		warn("typeof(instance) ~= \"Instance\"")
		return
	end
	if not transparency then
		transparency = 0
	end
	if typeof(transparency) ~= "number" then
		warn("transparency and typeof(transparency) ~= \"number\"")
		return
	end
	local transparency: number = transparency
	if transparency > 1 then
		warn("transparency > 1")
		transparency = 1
	end
	if transparency < 0 then
		warn("transparency < 0")
		transparency = 0
	end

	if instance:IsA("BasePart") then
		instance.Transparency = transparency
	end

	local descendants = instance:GetDescendants()
	for _, descendant in ipairs(descendants) do
		if not descendant:IsA("BasePart") then
			continue
		end
		descendant.Transparency = transparency
	end
end

The change I made is shadowing transparency with local transparency: number = transparency.

Let me know if it doesn’t fix it for you

1 Like

This fixed this particular case, but still… The type narrowing should be a thing. And also, if the type narrowing is not working, why is not working only for the second if. That;s wierd and I wanted to post this so the Roblox Team can see that they have bugs in the type checking systems. I hate to write extra code for something “useless” like this. That shadow is a ussles line of code. If the type narrowing works well, you should no longer need that shadow variable. But yes, that’s a cool solution. Thanks!

1 Like

I’m glad it works! You should see if there’s already a bug report for it. If not, make one!

Type Checking/Type Narrowing Gives Inconsistent type Errors for the same variable (NEW LUAU TYPE SOLVER - BETA FEATURES) - I made a BUG report

Why make life hard?
Why typeof() when ~=nil can be used instead?
It works well with early returns. I don’t know what you are talking about.

it works, yes, but the type error in the editor is bugged. have u enabled the BETA FEATURE? (the new typechecker) the old one works, yes.

also, typeof is mandatory, because the caller can give garbage value, like a string instead of a number.

That’s kinda weird - speaking about yourself in third person?
Anyway, is the caller in the room with us right now? :folded_hands: :skull:

Jokes aside, you’re literally the one using the function. You’re not “protecting” the code from anyone but yourself.
typeof isn’t mandatory here - it’s just bloat for a self-contained function.
Also, maybe send one reply instead of two next time.

If the project is made by one dev you are right, I am the only caller. But otherwise, it needs protection. When you work in a team you make the code as bulletproof as possible with warnings for debugging purposes, that’s why I said it is mandatory. Sry for double reply.

No, it’s literally not. You will not get anywhere with this. If you were to write an app like that in C++, for example (without macros), you would’ve been instantly kicked off the team and fired.
No any kind of runtime validation must exist of any kind unless mandatory.

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