I was wondering whether it is possible to create a decorator for a property which checks if value is assigned to it or not yet.
I want to create a required Decorator on a property. Suppose I have a directive and some properties need to have a value which is given when the directive is used/called.
@Directive({...})
export class xyz
@Input()
@Required()
get name() {...}
set name(s: string) {...}
}
Somewhere other part of app.
<div xyz>....</div>
This should throw an error because name property is missing, and it is kind of required in directive class.
<div xyz [name]="'anyName'">....</div>
I have tried couple but can't...
export function Required() {
return (target: {} | any, name: PropertyKey): any => {
const descriptor = {
get(this: any) {
console.log(this);
const propertyName = `__${String(name)}`;
if (!this[propertyName]) {
this[propertyName] = createId();
}
return this[propertyName];
},
enumerable: true,
configurable: true,
};
Object.defineProperty(target, name, descriptor);
};
But I can't acheive the goal.
I don't know how can I require a property via decorator. I know there is an other way using selector but I was wondering if this can be done by a decorator.
Thanks
Edit: At runtime is it possible to use the decorator?