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

A few improvements for functional programming #19857

Closed
na-sa-do opened this issue Nov 9, 2017 · 2 comments
Closed

A few improvements for functional programming #19857

na-sa-do opened this issue Nov 9, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@na-sa-do
Copy link

na-sa-do commented Nov 9, 2017

I'm writing some functional code and finding myself wanting a few type operators to smooth things over. They could also be used to type Function#apply, Function#call, etc. Since they're pretty closely related I'll just make one suggestion—if that doesn't work out, my bad.

argsof <func>

A tuple for the arguments taken by the function, for use with the spread operator or Function#apply. Using this on a non-function is illegal. If an argument is optional, the type just has | undefined added. (What about rest parameters? Maybe rest parameters could be added to tuples, as a way to override the default behavior of unioning all the member types. But that's a separate suggestion.)

returnof <func>

The type of the function's return value. Using this on a non-function is illegal.

Spread operator in function types

type Arguments = [number, string, number];

function call(...Arguments) {
}

This results in a call which takes a number, a string and a number as arguments. Those arguments aren't actually directly accessible to the function, since they don't have names -- they can be reached from the arguments object.

My main use for all of these is to mimic an existing function type dynamically, like this:

function wrap<T extends Function>(inner: T): T {
    return function(...argsof T): returnof T {
        return inner.call(this, ...arguments);
    };
}
@aluanhaddad
Copy link
Contributor

See also #5453

@mhegazy
Copy link
Contributor

mhegazy commented Nov 9, 2017

returnof tracked by #6606
argsof most usages should be tracked by #5453 and #12265

@mhegazy mhegazy added the Duplicate An existing issue was already created label Nov 9, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants