@eklabdev/blingts4
Version:
A comprehensive TypeScript decorators library implementing Stage 2 decorators for TS 4.9+
87 lines (81 loc) • 2.51 kB
JavaScript
/**
* Auto-generates a getter for a field
*/
export function Getter() {
return function (target, propertyKey) {
const fieldName = propertyKey;
const capitalizedName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1);
const getterName = `get${capitalizedName}`;
// Add the getter method to the class prototype
Object.defineProperty(target.constructor.prototype, getterName, {
value: function () {
return this[fieldName];
},
enumerable: false,
configurable: true,
writable: true,
});
};
}
/**
* Auto-generates a setter for a field
*/
export function Setter() {
return function (target, propertyKey) {
const fieldName = propertyKey;
const capitalizedName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1);
const setterName = `set${capitalizedName}`;
// Add the setter method to the class prototype
Object.defineProperty(target.constructor.prototype, setterName, {
value: function (value) {
this[fieldName] = value;
return this;
},
enumerable: false,
configurable: true,
writable: true,
});
};
}
/**
* Auto-generates a builder method for a field
*/
export function Builder() {
return function (target, propertyKey) {
const fieldName = propertyKey;
const capitalizedName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1);
const builderName = `with${capitalizedName}`;
// Add the builder method to the class prototype
Object.defineProperty(target.constructor.prototype, builderName, {
value: function (value) {
this[fieldName] = value;
return this;
},
enumerable: false,
configurable: true,
writable: true,
});
};
}
/**
* class Person {
@Getter()
name: string = '';
@Setter()
age: number = 0;
@Builder()
address: string = '';
constructor(name: string, age: number, address: string) {
this.name = name;
this.age = age;
this.address = address;
}
}
// Type augmentation for the decorated fields
type PersonWithHelpers = Person &
WithGetter<Person, 'name'> &
WithSetter<Person, 'age'> &
WithBuilder<Person, 'address'>;
// Usage with type casting
const person = new Person('John', 30, '123 Main St') as PersonWithHelpers;
*/