-
-
Notifications
You must be signed in to change notification settings - Fork 674
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
Decorate constructor arguments with access modifiers #495
Comments
For now, While it's possible to add support for parameters in constructor, it's an error prone pattern as you can easily missplace the parameter and swap If you really want to create a class instance, it's better to accept the anonymous object in constructor and call |
every pattern can be missused, but there are a lot of use cases where object types have very few arguments, or are constructed by a factory or builder pattern. just to have an example for a valid use case: @InterfaceType()
abstract class UpdateUserResult {
public constructor(
@Field(() => User)
public readonly user: User,
) {}
}
@ObjectType({ implements: UpdateUserResult })
class UpdateUserResultOk extends UpdateUserResult {}
@ObjectType({ implements: UpdateUserResult })
class UpdateUserResultRejected extends UpdateUserResult {
public constructor(
user: User,
@Field(() => String)
public readonly reason: string,
) {
super(user);
}
} |
Should be fairly easy to implement, moving to |
this feature is very welcome to easily form up responses in queries mutations etc |
Any progress on this? Quality of Life feature :) |
I've checked and unfortunately this is not possible 😞 When using decorator on constructor argument, it's treated like a function parameter decorator. The only solution would be to duplicate the property name in the decorator options: @ObjectType()
export class User {
constructor(
@Field(() => Int, { propertyName: "id" })
public id: number,
@Field({ propertyName: "username" })
public username: string,
) { }
} Which makes no sense, it's same duplication like now with defining constructor params apart from the fields definitions. |
Is your feature request related to a problem? Please describe.
We are using extensively cool TypeScript feature which allows parameters from constructor map to class members, Example:
then
Describe the solution you'd like
I would like to be able to decorate these parameters as well
Describe alternatives you've considered
Don't use this TS feature
The text was updated successfully, but these errors were encountered: