`never` types can be cast to anything

Reproduction Steps

Copy this source into a script:

Source Code
--!strict

local value: string|number = "foo";

if type(value) == "string" then
	local s: string = value; --not an error -- good
	local n: number = value; --this should be an error
elseif type(value) == "number" then
	local s: string = value; --this should be an error
	local n: number = value; --not an error -- good
else
	local s: string = value; --this should be an error, but it isn't
	local n: number = value; --this should be an error, but it isn't
end

Expected Behavior

I’d expect an error on lines 12 & 13, as we’re casting an impossible value (type never) into a string & number, respectively.

Actual Behavior

I get no errors on those lines.

Also, note that you do get errors on line 7 & 9, which is expected, as we’ve narrowed the type down from string|number, and hence those assignments are invalid.

RobloxStudioBeta_KIyxEFLgjy

Workaround

There’s not really a workaround required, you just get worse typechecking because of it.

Issue Area: Studio
Issue Type: Other
Impact: Low
Frequency: Sometimes

Create an issue on the Luau Repo for a faster response

Thanks for the report! We’ll take a look.

2 Likes