question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Extract function parameters or change function return type in mapped types

See original GitHub issue

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)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:8
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
Keyvescommented, Oct 26, 2018

like this?

type MapResultToPromise<T> = T extends (...args: infer U) => infer R ? (...args: U) => Promise<R> : T;
3reactions
Cerberuscommented, Feb 15, 2019
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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Mapped Types - TypeScript
Generating types by re-using an existing type.
Read more >
Create a function with a specific mapped type as return type
The parameter relations must be its own generic type. Otherwise it is just keyof T but not the actually values you passed to...
Read more >
Mastering TypeScript mapped types - LogRocket Blog
type Currency = [number, string]; const amount: Currency = [100, 'USD']; function add(values: number[]) { return values.reduce((a, b) => a + ...
Read more >
Extract parameter types from string literal types with TypeScript
Whenever you want to define the type of a function in TypeScript, where the argument types and the return type depends on each...
Read more >
Advanced TypeScript: Mapped Types and more - Medium
as const expression; keyof and typeof; type argument inference for ... Now we can implement a function that will return us the full...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found