Remove decorator when extending class in Typescript

Viewed 121

I have a class which have some decorator I want when extend this class it remove the decorator.

For example:

class User {
  @IsNotEmpty()
  username: string;

  @IsNotEmpty()
  name: string;
}

class UpdateUser extends User {
  
}

All decorator exist in UpdateUser class which I don't want. Specifically I'm using class-validator I don't need IsNotEmpty decorator when updating.

Is there any way to remove the decorator when extending class?
Is there any way to replace decorator with other when extending class?

1 Answers

There is no possible way to achieve what you want because these decorators are not only TypeScript things, they also generate JavaScript code which adds side effects at run time (which can be a mutation of the class, and/or some other external effect).

The only thing you could do (very ugly, I strongly advise against it) is to find the actual effects of these decorators to revert them with your own code.

Related