@eklabdev/blingts4
Version:
A comprehensive TypeScript decorators library implementing Stage 2 decorators for TS 4.9+
51 lines (45 loc) • 1.39 kB
TypeScript
/**
* Auto-generates a getter for a field
*/
export declare function Getter(): (target: any, propertyKey: string) => void;
/**
* Auto-generates a setter for a field
*/
export declare function Setter(): (target: any, propertyKey: string) => void;
/**
* Auto-generates a builder method for a field
*/
export declare function Builder(): (target: any, propertyKey: string) => void;
type Capitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : S;
export type WithGetter<T, K extends String> = {
[P in K as `get${Capitalize<string & P>}`]: () => any;
};
export type WithSetter<T, K extends String> = {
[P in K as `set${Capitalize<string & P>}`]: (value: any) => T;
};
export type WithBuilder<T, K extends String> = {
[P in K as `with${Capitalize<string & P>}`]: (value: any) => T;
};
export {};
/**
* 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;
*/