reflect-metadata icon indicating copy to clipboard operation
reflect-metadata copied to clipboard

Parameter decorator names

Open j opened this issue 7 years ago • 1 comments

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?

j avatar Feb 22 '18 22:02 j

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.

rbuckton avatar Feb 22 '18 22:02 rbuckton

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

asngeo avatar Apr 11 '23 14:04 asngeo

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.

rbuckton avatar Dec 14 '23 21:12 rbuckton