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.

[Question] Use nameof inside decorator

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dsherretcommented, Mar 6, 2019

@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.

0reactions
lonix1commented, Mar 6, 2019

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!

Read more comments on GitHub >

github_iconTop 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 >

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