Casting Vector3, Vector2, etc to generic lerpable type

I’m currently writing a simple module which takes in a list of time stamps and lerpable values to create a path.
I’d like for this module to handle not only Vector3s and CFrames, but also any other value that can be :Lerp()ed now and in the future, without modifying the source code again.
To achieve this goal, I’ve created a value type, which is defined as follows:

type value = {
	Lerp: (value, value, number) -> value
}

I’d imagine that Vector3s and the like could easily be converted to this type, as the type value does not contain any information that a Vector3 does not contain. This doesn’t appear to be the case though, as roblox issues the warning Type 'Vector3' could not be converted into 'value'.

I’ve attempted to make the type value an union between the definition above and any, and also making it either of the two, but neither of these achieve what I want.
Making an union only makes the error message needlessly complicated without giving any new information, while making the type either any or the definition above will not give autocorrection at all.

The following piece of code has this issue isolated, and could make testing a bit faster if need be.

--!strict
type value = {
	Lerp: (value, value, number) -> value
}

local a: Vector3 = Vector3.new(1,1,1)
local b: value = a -- This should have the warning

It probably has something to do with Roblox turning Vector3 into a primitive type, which means that it’s no longer considered just an immutable table and is now its own separate datatype

For example, RaycastResult is a defined data type and yet it’s still internally a table type:

--!strict

type alsoRaycastResult = {
	Instance: Instance,
	Position: Vector3,
	Distance: number,
	Material: Enum.Material,
	
	--apple: boolean
}

local ray: RaycastResult = workspace:Raycast(Vector3.zero, Vector3.one)
local ray2: alsoRaycastResult = ray

There would be no warnings until you uncomment the apple: boolean index

1 Like

I do believe that this would be the cause of the problem.
I’ve done a little bit more testing, and found that pretty much all values that I need to be lerpable, such as Color3, Vector2, UDim2, etc, have gotten the primitive treatment or something along those lines.
I’ll mark your comment as the solution, thanks for your help!

1 Like