Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intrinsic/utility types, like TypeScript (feature request - mostly for function generics) #124

Open
LoganDark opened this issue Apr 11, 2022 · 1 comment

Comments

@LoganDark
Copy link

LoganDark commented Apr 11, 2022

TypeScript has types/aliases like Parameters<...> which accept a function and resolve to their arguments, or ReturnValue<...> which does the same for return value, etc. as well as other cool things like Exclude and so on. I don't think this plugin will ever quite reach TypeScript's power, but I recently came across a situation where I was typing a pcall-like function, and would've liked to express that it takes the same extra arguments that the function itself takes.

Would also probably require a new annotation for declaring the entirety of a vararg (as a TyMultipleResults ) rather than the type of each element of the vararg.

Quick demonstration:

-- `varargs` annotation below. as opposed to `vararg` which specifies the type of each vararg,
-- `varargs` specifies the type of all of the entire varargs

--- @generic F: function
--- @param func F @function to spawn in a new thread
--- @varargs Parameters<F> @parameters to call the function with
--- @return ReturnValues<F>
function thread.spawn(func, ...) end
thread.spawn(function(a, b, c)
    -- a, b, c are all inferred as number due to the below parameters
end, 1, 2, 3)

thread.spawn(some_func, 1, 2,3) -- either some_func is required to take (number, number, number), or the arguments after some_func 

Would also make pcall more accurate

@Xseba360
Copy link

Something like Partial<Type> , Omit<Type, Keys> and Exclude<UnionType, ExcludedMembers> would be very handy

https://www.typescriptlang.org/docs/handbook/utility-types.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants