Type checking an array of elements of the same type

I want to typecheck an array that is either all Vector2s, all Vector3s, or all CFrames. How would this syntax look?

Currently, I am doing
points: {Vector2 | Vector3 | CFrame}
which is giving some weird warnings.
image

I have also tried
points: {Vector2} | {Vector3} | {CFrame}
which still gives warnings.
image

I’m pretty sure it gives that error because, in general, it’s unspecified whether the list contains more than 0 values. For all the type checker knows, the list can contain 0 of the values in the table, and cause an error.

However, formatting the type like so:

local function hello(points: {Vector2 | Vector3 | CFrame})
	if #points > 0 then
		
	end
end

does seem to resolve the errors thrown by referencing ‘points’ as a table.

1 Like

I solved this by narrowing, in other words, redefining a type to be a more specific type by using type guards in this example below:

--!strict

local points: { Vector3 | Vector2 | CFrame } = { Vector3.new(), Vector2.new() }

local point: (Vector3 | Vector2 | CFrame) = points[1]

if typeof(point) == "Vector3" then
	point = point:Lerp(point, 1)
elseif typeof(point) == "Vector2" then
	point = point:Lerp(point, 1)
elseif typeof(point) == "CFrame" then
	point = point:Lerp(point, 1)
end

Did that, all the warnings are gone and it works; however, I am uncertain as to why this occurs. It seems to point out that the union of the type definitions of the Lerp functions of all 3 types and says it’s a non-function?

This seems to be the correct solution for now. I’ve been told off the forum that luau doesn’t recognize union functions yet.