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

Make property decorator fabrics share generics with corresponding methods #58649

Closed
6 tasks done
finom opened this issue May 24, 2024 · 2 comments
Closed
6 tasks done

Comments

@finom
Copy link

finom commented May 24, 2024

πŸ” Search Terms

decorator generic
decorator set type

βœ… Viability Checklist

⭐ Suggestion

There are multiple requests to be able to modify class or its methods type

#4881
#49229

But I haven't found a much simpler request: be able to share property decorator fabric generic with its method.

πŸ“ƒ Motivating Example

There is the decorator fabric example, nothing fancy:

function paramDecoratorFabric<T>(arg: T) {
  return function (target: any, propertyKey: string) {
    console.log('Parameter Decorator called on: ', arg);
  }
}

And there is what I would be happy to be able to achieve:

class ParameterDecoratorExample {
  method<T>(@paramDecoratorFabric<T>('Hello') param: T) {
    return param;
  }
}

Unfortunately, I get the following error:

image

Which says that the T of @paramDecoratorFabric is kind of a different thing that doesn't belong to the "place" where it was used.

πŸ’» Use Cases

I'm actively working on my RPC-ish library and one of the biggest downsides comparing to other RPC libraries is inability to automatically recognise Zod types provided at the controller. This requires to add the type to the controller method manually but also to create extra variables.

export default class UserController {
    @put()
    @vovkZod(UpdateUserModel, UpdateUserQueryModel)
    static updateUser(
        req: VovkRequest<z.infer<typeof UpdateUserModel>, z.infer<typeof UpdateUserQueryModel>>
    ) {
        // ...
    }
}

I understand that adding ability to modify method/class type by a decorator is quite challenging. But I hope that changing the "scope" of generics isn't that big deal. With the feature I'm requesting I would be able to achieve the goal without defining extra variables:

export default class UserController {
  @put()
  static updateUser<B, Q>(
    @vovkZod<B, Q>(z.object({ /*...*/ }), z.object({ /*...*/ })) req: VovkRequest<B, Q>
  ) {
      // ...
  }
}

The method and the parameter already share B and Q, and since @vovkZod is used "inside" of this definition, it makes sense for it to be able to also use B and Q.

@MartinJohns
Copy link
Contributor

Duplicate of #2685. The reasoning still holds, and ECMAScript decorators don't even support parameter decorators.

@finom
Copy link
Author

finom commented May 24, 2024

@MartinJohns right, that's a duplicate. Thanks for the link.

@finom finom closed this as completed May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants