I’m making this function to calculate the internal angle of three points:
function InternalAngle<T>(p1: T, p2: T, p3: T): number
-- https://youtu.be/vUQo7HVCx8A
local p1p2 = p2 - p1
local p1p3 = p3 - p1
return math.acos(p1p2:Dot(p1p3) / (p1p2.Magnitude * p1p2.Magnitude))
end
However, luau is complaining that Type 'T' could not be converted into 'number'
.
How do I make it clear that T must be either a Vector2
or a Vector3
?
Somewhat solved this:
--!strict
function InternalAngle<T>(p1: T & (Vector2 | Vector3), p2: T & (Vector2 | Vector3), p3: T & (Vector2 | Vector3))
-- https://youtu.be/vUQo7HVCx8A
local p1p2 = p2 - p1
local p1p3 = p3 - p1
return math.acos(p1p2:Dot(p1p3) / (p1p2.Magnitude * p1p2.Magnitude))
end
InternalAngle(Vector3.new(0, 0, 0), Vector3.new(0, 0, 0), Vector3.new(0, 0, 0))
-- no warning
InternalAngle(Vector3.new(0, 0, 0), Vector2.new(0, 0), Vector3.new(0, 0, 0))
-- Type 'Vector2' could not be converted into type 'Vector3'
InternalAngle(Vector2.new(0, 0), Vector2.new(0, 0), Vector3.new(0, 0, 0))
-- Type 'Vector3' could not be converted into type 'Vector2'
This does, however, make the entire function body very angry.
It keeps thinking that p1p2
and p1p3
is a number for some reason.
I don’t care though.