Help with Typechecking

I am trying to make a function that takes in any function as its first parameter and then returns that same function But I cannot figure out the type checking for it

image

image

I could do

function foo<A..., R...>(func: (A...) -> (R...)): (A...) -> (R...)
	return func
end

But it does not support arguments and also it is too long and unreadable
image

Pls help! Any support would be appreciated

You could try letting the type solver infer it.

function foo<A..., R...>(func: (A...) -> (R...))
	return func
end

Yeah but now it is not showing arguments

image

Does adding an “?” to the first R fix it? (in the previous code)

What do you mean?

I’m not sure if the type solver is able to do that.

image

Now it is just doing this

image

(I put an extra ? at the end of the Second R because it wouldn’t stop warning without it)


Seems like it does carry arguments over (somewhat), but no documentation.

1 Like

The only way to do it, is like this:

function a<Args..., Rets...>(f: (Args...) -> (Rets...)): (Args...) -> (Rets...)
	return f
end

local b = a(function(hi: string): number 
    return 0 
end)

b() -- (string): number

This method does support the types of the arguments.

1 Like

You do lose all the argument names and documentation unfortunately.

1 Like

That is intentional for now. Function types do not hold the information of the argument names, only the types. And that is enough in most cases.

1 Like

That is just the same thing as this

Also I think I kind of found a solution

function foo<T> (func: T & (...any) -> (...any)): T
	return func
end

But the only problem is that It autocompletes the members of the first parameter even if it is not a function

But At least it warns the user if the first parameter is not a function

I will not mark this as solution yet because there is Probably a better way to do this