[Typed Luau] In strict mode, type refinement using a truthy check doesn't work without an explicit type annotation

When in strict mode in typed Luau, refining the type of a value using a simple if foo then guard doesn’t work unless foo has an explicit type annotation. Using all other method of type refinement work just fine.

As an example, the following code should be type safe, but according to the type checker, it is not.

--!strict
local foo: number? = 1

local bar = foo

if bar then
	local baz: number = bar
end

However, adding a type annotation to bar causes it to become safe according to the analyzer:

--!strict
local foo: number? = 1

local bar: typeof(foo) = foo

if bar then
	local baz: number = bar
end

This doesn’t change the types at all but fixes a script analysis error. This is particularly important for table types, as you can’t check against them using any of the other type refinements, so something like { }? can’t be refined otherwise.

This occurs in studio version 0.445.0.410316.

2 Likes

Thanks for the report! We’ve filed this internally and we’ll follow up here when we have an update for you.