Luau TypeError: Silencing "type could be nil"

function test(players: {[number]: Player}?)
    if players == nil then
        players = Players:GetPlayers(); -- Assigned if nil.
    end
    -- players variable is no longer possible to be nil.

    for a=1, #players do
        print(players[a])
    end
end

image

How do I silence this after assigning the optional variable?

A couple options, none of which are great, but hopefully in the future the type system is improved to resolve issues like this.

1- Overwrite players to use the inferred type returned from players or Players:GetPlayers(), one-liners will work while full on if-statements don’t

function test(players: { Player }?)
	players = players or Players:GetPlayers()

	for a=1, #players do
		print(players[a])
	end
end

2- Assert players is truthy

function test(players: { Player }?)
	if players == nil then
		players = Players:GetPlayers()
	end
	
	assert(players)

	for a=1, #players do
		print(players[a])
	end
end

3- Use a generic for loop and use a type cast when trying to iterate over players

function test(players: { Player }?)
	if players == nil then
		players = Players:GetPlayers()
	end
	
	for _, player in players :: { Player } do
		print(player)
	end
end

however do note that aside from that one line, players will still be nil-able