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

Allow decorators (and metadata) on non-class functions #36049

Closed
5 tasks
Roaders opened this issue Jan 7, 2020 · 1 comment
Closed
5 tasks

Allow decorators (and metadata) on non-class functions #36049

Roaders opened this issue Jan 7, 2020 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@Roaders
Copy link

Roaders commented Jan 7, 2020

Search Terms

function decorators

Suggestion

Decorators and the metadata that they produce are great. As far as I can see this is only supported on classes, class properties and class functions.
I would like to be able to decorate anything. Specifically I would like to decorate a function that is not defined in a class so that at runtime I can get metadata about the types of parameters it expects.

Use Cases

Class metadata can be used by DI systems to inject constructor params. I would like to write code that does the same for a function. Specifically I would like to write a DI system for a React Function component to resolve any parameters (or dependencies) that function has.

Examples

@storeMetadata()
function myReactFuntion(params: IParams, service: SomeService, util: SomeUtil){
    return someReactInstance();
}

function performInjectMagic(func: (...args: any[]) => any){
    return (params: IParams) => {
         const diArgs = evaluateAndInstantiateBasedOnMetaData(func);

         return func(params, ...diArgs);
   }
}

export const performInjectMagic(myReactFunction);

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@MartinJohns
Copy link
Contributor

Duplicate of #7318. And this would first require JavaScript to support function decorators.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Jan 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants