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

"design:paramnames" metadata key #4905

Open
remojansen opened this issue Sep 21, 2015 · 11 comments
Open

"design:paramnames" metadata key #4905

remojansen opened this issue Sep 21, 2015 · 11 comments
Labels
Domain: Decorators The issue relates to the decorator syntax In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@remojansen
Copy link
Contributor

From #2589

A few notes on metadata:

  • Type metadata uses the metadata key "design:type".
  • Parameter type metadata uses the metadata key "design:paramtypes".
  • Return type metadata uses the metadata key "design:returntype".

Can you please add support for a "design:paramnames" metadata key? It would return the design-time names of the arguments of a function

I can convert a function to string at runtime using Function.prototype.toString() and then use a couple of Regex to get the parameter names:

STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
ARGUMENT_NAMES = /([^\s,]+)/g;

But when a minifier/compressor is used the param names become "a", "b" ...

I would like to have a way to access the design-time names of the arguments of a function at run-time even after compression.

@danquirk danquirk added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Sep 21, 2015
@RichiCoder1
Copy link

If I may ask, what's the use case of this?

@remojansen
Copy link
Contributor Author

@RichiCoder1 Sure, I'm working on a inversion of control container and I want to add contextual binding support. One of the things that I want to allow developers to do is to use constraints based on the injection target. Take for example:

class Soldier {

  private _mainWeapon : IWeapon;
  private _secondaryWeapon : IWeapon;

  constructor(main : IWeapon, secondary : IWeapon) { // injections
    this._mainWeapon = main;
    this._secondaryWeapon = secondary;
  }
}

When I declare the bindings I can use a constraint to resolve ambiguous bindings:

kernel.bind(new Binding<IWeapon>(Katana).when((target) => { return target.name === "main" }));
kernel.bind(new Binding<IWeapon>(Shuriken).when((target) => { return target.name === "secondary" }));

My problem is that If the javascript is compressed, the value of target.name would be "a" and "b" and the IoC container would not be able to resolve the service.

@RichiCoder1
Copy link

Ahh, that would make sense.

@mhegazy mhegazy added In Discussion Not yet reached consensus and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Sep 28, 2015
@Joshua-F
Copy link

Joshua-F commented Jan 1, 2016

I would love to see this added.

@remojansen
Copy link
Contributor Author

Hi @mhegazy It's been a while since this was tagged as In Discussion. Do you have an update on this? Thanks!

@mhegazy
Copy link
Contributor

mhegazy commented Jan 28, 2016

We do not intend to make any changes to decoratos implementation until the TC39 discussions about the current proposal are finalized. there will be some changes that we need to make with the latest draft of the proposal; after that is done, we can look into adding these additional feature requests.

@isuda
Copy link

isuda commented Jan 9, 2017

@mhegazy - Its been a year since your last comment on this. Any updates on when this might be able to be implemented? It would be super useful to be able to use this to do DI when code has been minified.

@mhegazy mhegazy added the Domain: Decorators The issue relates to the decorator syntax label Nov 23, 2017
@sdc395
Copy link

sdc395 commented Dec 8, 2017

I'd also love to see this implemented as it would allow me to map the properties of an object (input parameters from a GraphQL query) on to the parameters of a function (the resolver). I was hoping to find a way to augment the metadata emitted by tsc but it seems compiler plug-ins are not yet a thing.

@sdc395
Copy link

sdc395 commented Dec 8, 2017

While we're at it, knowing the type a promise will return would also be very useful so metadata for generics too, please.

@raymondfeng
Copy link

+1. At LoopBack, we introduce decorators to annotate method parameters to describe how to map methods to REST APIs. It will be great that we can infer parameter types/names from the design time metadata. For example:

@returnValue()
async createOrder(@param() order: Order): Promise<Order> {
  // ...
}

We would like to know:
- The parameter name ('order') N/A today
- The parameter type (Order) (It's already supported, but not for array item types)
- The return type (Promise) but we care more about `Promise<Order>`

@nodkz
Copy link

nodkz commented Apr 9, 2019

Duplicate #12626

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Decorators The issue relates to the decorator syntax In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

9 participants