Any way to restrict the possible types of a Generic/Template?

Dose anyone know of a way to restrict the possible types for a Generic? An example of this would be:

function foo<T: Vector2 | Vector3>(mainPoint: T, points: {T}): T
    ...
end


If not i would be fine to just do this instead:

type Vector = Vector2 | Vector3

function foo(mainPoint: Vector, points: {Vector}): Vector
    ...
end

As far as I know, Luau currently does not support generic constraints. There are a couple RFCs for them though, and they do plan on implementing them. I don’t think there’s an exact timeline for it though

2 Likes

Late response here, but I’ve seen that combining it as a union type with its preferred type seems to work pretty well.

function module.stringer<T>(value: ("a" | "b" | "c") & T)
	
end

module.stringer("a")
module.stringer("b")
module.stringer("foo") -- Type Error: Type '"foo"' could not be converted into '("a" | "b" | "c") & string' caused by: Not all intersection parts are compatible. Type '"foo"' could not be converted into '"a" | "b" | "c"'; none of the union options are compatible