Parameter decorator names
Right now, the only info known for parameter decorators is, target, methodName, and it's index. Is it possible to get the parameter variable name?
i.e.
class Foo {
bar(@Baz() haz: string): string {
// ...
}
}
I want to get the string haz from the @Baz decorator. Possible?
Not without parsing the function, and the results of Function.prototype.toString() aren't reliable in all editions of JavaScript, not to mention the complexity of parsing out names from binding patterns. Also, parameter names can change due to other decorators wrapping the method, etc. Generally the only information you can rely on is the ordinal position of the parameter in the top-level parameter list.
Not without parsing the function, and the results of
Function.prototype.toString()aren't reliable in all editions of JavaScript, not to mention the complexity of parsing out names from binding patterns. Also, parameter names can change due to other decorators wrapping the method, etc. Generally the only information you can rely on is the ordinal position of the parameter in the top-level parameter list.
Hi @rbuckton ,
Is it possible to parse the param names from the source ts file and put them into the reflect-metadata? I think the param names are useful as we can leverage these information to implement some features like DI and deserialization.
Thanks
That is out of scope for reflect-metadata, as this package does not control what is available to decorators itself. TypeScript's --experimentalDecorators do not provide this, though I am considering it for https://github.com/tc39/proposal-class-method-parameter-decorators.