Conditional type checking in Luau?

I’m still not very confident with Luau, and had a question. Is it possible to assign the type properly in this case?

type TestTable = {['Hello']: string};

local function Test(a: number): (boolean, TestTable?)
	if (a > 0) then
		return true, {['Hello'] = 'world'};
	else
		return false, nil;
	end
end

local function OtherTest()
	local Success, Output = Test(2);
	if (Success) then
		print(Output.Hello) -- "Value of type 'TestTable?' could be nil"
	end
end

Right now, if “Success” is true, then the Output MUST be the table, but the type checker still thinks it could be nil and give a type warning. Is there a correct way of handling this situation? Or some way of incorporating conditions in the type definitions?

1 Like

Unfortunately, this does not exist at the moment. But there’s a feature request you can show support toward:

But, there is a funky workaround you can try:

1 Like

Ah that’s frustrating. Thanks for responding.

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