How would I typecheck a table as an argument within a callback?

I’ve searched around on the forum and I really haven’t found something specific to my question. I have this

function Attach<T...>(Parent: T, Callback: ({ T }) -> ({ T })): ({ T })
    ...
end

and this is how I want it to be used

Attach(..., function({ Animator, Animation })
    ...
end);

, but it just errors because it doesn’t expect the {

What exactly are you trying to accomplish? Is the generic T for Parent supposed to contain {T} for both your callback and returned values?

  1. You are using a variadic generic type of T... but as a regular generic type. It’s used for, well, variadic values.

  2. I find it confusing why you are using both a generic and a table for your callback. All {T} is going to return is an array only containing whatever you pass as the Parent argument.

  3. Can you explain to me how this function is supposed to work.

I’m trying to share values from other places. This is what is inside of the function

local Animator: Animator, Animation: Animation;

do
	for _, Descendant: any in Parent:GetDescendants() do
		if Descendant:IsA("Animator") then

			Animator = Descendant;
		elseif Descendant:IsA("Animation") then

			Animation = Descendant;
		end
	end
end

return Callback(Animator, Animation);

I managed to figure out what to do:

return {
	
	Attach = function<P, C...>(self: {}, Parent: P, Children: { string }, Callback: (C...) -> (C...)): (C...)
	
		local Descendants: { string } = {};
		for _, Descendant: any in Parent:GetDescendants() do
			for i = 1, #Children do
				if Descendant:IsA(Children[i]) then
					
					table.insert(Descendants, Descendant);
				end
			end
		end
		return Callback(table.unpack(Descendants));
	end
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.