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

Feature Request: Explicit destructuring function types #18808

Closed
ccorcos opened this issue Sep 27, 2017 · 4 comments
Closed

Feature Request: Explicit destructuring function types #18808

ccorcos opened this issue Sep 27, 2017 · 4 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision

Comments

@ccorcos
Copy link

ccorcos commented Sep 27, 2017

TypeScript Version: 2.5.2

Code

Most of the time Typescript does a great job inferring types so its possible to write a higher-order function with generic types representing the inputs and outputs a function. For example:

// Infers the inputs and outputs of a function
function f<X, Y>(fn: (x: X) => Y): (x: X) => Y {
	return fn
}

function g(x: number): string {
	return x.toString()
}

// result has type (x: number) => string as we'd expect
const result = f(g)

However, sometimes Typescript does not infer types well, such as #18807. In cases such as this, how are we supposed to explicitly define the generics of the function f?

The obvious example is f<number, string>(g) but that can be very tedious, especially the input type definition for g isn't defined separately and exported or if the output type of g is inferred.

A solution to this problem would be some way of destructuring the type of a function so I can pass typeof g as the generic argument to f like the following:

function f<(x:X) => Y>(fn: (x:X) => Y): (x: X) => Y {
	return fn
}

f<typeof g>(g)
@DanielRosenwasser
Copy link
Member

@rbuckton has been experimenting with some stuff here, but I don't think we have a formal writeup/proposal I can point to yet.

@ccorcos
Copy link
Author

ccorcos commented Sep 28, 2017

Ok. It just seems weird that we can explicitly do the same that that the compiler can infer...

@mhegazy
Copy link
Contributor

mhegazy commented Jul 17, 2018

With conditional types you should be able to extract these types from the function, and no need for special destructuring operations.. e.g.:

f<ParamType<typeof g>, ReturnType<typeof g>>(g);

type ParamType<T> = T extends (a: infer A) => any ? A : never; 
type ReturnType<T extends (...args: any[]) => any> = T extends (...args: any[]) => infer R ? R : any;

@mhegazy mhegazy added the Declined The issue was declined as something which matches the TypeScript vision label Jul 17, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision
Projects
None yet
Development

No branches or pull requests

4 participants