Extract function parameters or change function return type in mapped types
See original GitHub issueSearch 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:
- Created 5 years ago
- Reactions:8
- Comments:5 (1 by maintainers)
Top Results From Across the Web
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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
like this?
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.So then we get a chain-proxy object as you want