Décorateurs de Propriété
const ReadOnly = () => {
/* Return the decorator. */
return (target, key: string) => {
Object.defineProperty(target, key, {
set: () => {
throw new Error(`${target.constructor.name}.${key} is read only`);
}
});
};
};
class Customer {
@ReadOnly() firstName: string;
}
const customer = new Customer();
customer.firstName = 'Foo';Mis à jour