[Question] Use nameof inside decorator
See original GitHub issueI’m using the inversify IOC container.
This is used in controllers’ action methods:
@httpGet("/:id")
public async getUser(@requestParam("id") id: number): Promise<User> { // <----- "id"
// ...
}
None of these things are available in that context: this
, arguments
, id
.
Is there some trick to avoid that "id"
magic string in the argument list?
A workaround:
const names = {
getUser: {
id: "id"
}
};
And then: nameof(names.getUser.id)
. But that’s not ideal.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
referring to the name of the decorated function inside decorator
The original name of a function is stored in the .__name__ attribute: >>> def foo(): pass ... >>> foo.__name__ 'foo' >>> bar =...
Read more >Why can't nameof be used with protected or private members ...
The nameof operator only works with class members that are public. This means that when used with reflection methods the member name needs ......
Read more >Decorator to print Function call details in Python
We will be using the co_varnames attribute that returns the tuple of names of arguments and local variables and co_argcount that returns the ......
Read more >Learn How nameof operator work in C# with Sample Code
This is a guide to C# nameof. Here we discuss the working of nameof operator in C# and examples for better understanding.
Read more >What is name of this design that looks like a chemistry ...
I would simply call it a Hexagon pattern/layout or perhaps a Honeycomb layout.
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
@lonix1 I think ideally the library itself should be using reflect-metadata and then it should make the parameters default to implicitly have
@requestParam("parameterNameGoesHere")
. You may want to open an issue in their project and ask them if that’s possible.I miss reflection too. I spend most of my time in c# code outside of ts/js.
Thanks for telling me about
ts-morph
, it looks really cool!Your idea of a compiler transform is really interesting, I’m going to play around with that and see if I can come up with something. In .net there is something “similar” (t4 templates) which are basically design-time code generators (code that generates code). Maybe that’s something I’ll look into.
I know I sound like every other newcomer to node, but… I really miss net/java’s reflection… especially in testing!
Thanks for all the tips. If I come up with something I’ll post back here!