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

Extract function parameters or change function return type in mapped types #24068

Open
Roaders opened this issue May 11, 2018 · 5 comments
Open
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Roaders
Copy link

Roaders commented May 11, 2018

Search Terms

extract function parameters
change function return type

Suggestion

Allow mapped types to change the return type of a function

Use Cases

I am writing a mocking framework. My system under test might be like this:

MyClass{
    myClassFunction(paramOne: string, paramTwo: number): string{}
}

but in my mocking framework I want to do this:

mockedObject
    .withFunction("myClassFunction")
    .withParameters("paramOneValue", 123)
   .wasCalled();

to do the withParameters I need to type a function with the same parameters as defined on my class but with a different return type.

Checklist

My suggestion meets these guidelines:
[ ] This wouldn't be a breaking change in existing TypeScript / JavaScript code
[ ] This wouldn't change the runtime behavior of existing JavaScript code
[ ] This could be implemented without emitting different JS based on the types of the expressions
[ ] This isn't a runtime feature (e.g. new expression-level syntax)

@mhegazy
Copy link
Contributor

mhegazy commented May 14, 2018

Discussed in #23045. we need a few basic concepts to be added to the language before enabling such scenarios. we first need variadic types (tracked by #5453), we need some concept of an open-ended tuple, and we need a way to covert the function param list to tuples and vice versa.

@mhegazy mhegazy added the Suggestion An idea for TypeScript label May 14, 2018
@Roaders
Copy link
Author

Roaders commented May 14, 2018

Thanks for the reply... Seems like it's quite a way off...
I assume when all these pieces come into place well be able to map the parameters.
I.e. convert a list of params to a list of ISomeType

@cyanql
Copy link

cyanql commented Oct 26, 2018

like this?

type MapResultToPromise<T> = T extends (...args: infer U) => infer R ? (...args: U) => Promise<R> : T;

@weswigham weswigham added the Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature label Nov 6, 2018
@Cerberus
Copy link

type ArgumentTypes<T> = T extends (... args: infer U ) => infer R ? U: never;
type ReplaceReturnType<T, TNewReturn> = (...a: ArgumentTypes<T>) => TNewReturn;

I found this on stackoverflow. It's very useful.
ref: https://stackoverflow.com/a/50014868/8491995

And I've tried to apply Proxy to object.

type Proxify<T> = { [P in keyof T]: ReplaceReturnType<T[P], Proxify<T>> }

function proxify<T>(o: T): Proxify<T> {
	return new Proxy(o, handler)
}

So then we get a chain-proxy object as you want

@thorn0
Copy link

thorn0 commented Feb 21, 2020

This all is possible now (except probably for some tricky cases). Why is the issue still open?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

6 participants