@bollo-aggrey/ts-autogen
v1.1.0
Published
Lightweight Lombok-like decorators for TypeScript
Maintainers
Readme
TypeScript Autogen
🚀 Lombok-style decorators for TypeScript — reduce boilerplate for getters, setters, builders, and related helpers. Works with both ESM and CommonJS modules.
✨ Features
- 🏗️
@Data– Generates getters, setters,toString, andequals - 🔧
@Builder– Fluent builder pattern with type-safe method chaining - 📝
@Getter/@Setter– Fine-grained property access control - 🔍
@ToString/@Equals– Object string and equality helpers - 🏭
@AllArgsConstructor/@NoArgsConstructor/@RequiredArgsConstructor– Flexible constructor generation - 🔒
@Value– Immutable objects withreadonlyproperties - 🎯 TypeScript: opt-in typings for generated APIs via
autogen/autogenDataBuilder(see below) - 🔄 Automatic version checking to keep your decorators up-to-date
What was fixed
autogen()typing – Previouslyautogenwas typed to returnany, so editors could not completebuilder(), getters, setters, etc. You now pass a small feature map (withas const) that matches your decorators, or useautogenDataBuilder()for the common@Data()+@Builder()stack. Exported types includeAutogenFeatures,AutogenClass,AugmentedInstance, andFluentBuilderFor.- README accuracy – Decorators apply behavior at runtime (prototype / class wrapping), not via a separate compile-time code generator. Earlier wording implied zero runtime cost and compile-only transforms; that was incorrect.
npm teston Windows – The test script no longer relies on Unix-styleNODE_OPTIONS='...'; it usesnode --import tsxso tests run on Windows and Unix.- Stray markdown – Removed a duplicate closing fence in the Advanced Usage example.
1.1.0 ships the typed autogen / autogenDataBuilder APIs, README corrections, Windows-friendly npm test, and the related cleanup.
📦 Installation
npm install @bollo-aggrey/ts-autogen
yarn add @bollo-aggrey/ts-autogen
pnpm add @bollo-aggrey/ts-autogen💡 The package includes automatic version checking to ensure you're always using the latest features and bug fixes. You'll be notified in your console if an update is available.
🚀 Quick Start
Basic Usage
import {
Data,
Builder,
AllArgsConstructor,
Getter,
Setter,
autogenDataBuilder
} from '@bollo-aggrey/ts-autogen';
@Data()
@Builder()
@AllArgsConstructor()
class ProductClass {
@Getter() @Setter() name: string = '';
@Getter() @Setter() price: number = 0;
@Getter() @Setter() category: string = '';
public save(): string {
return `Saved product: ${this.name}`;
}
}
export const Product = autogenDataBuilder(ProductClass);
const laptop = Product.builder()
.name('MacBook Pro')
.price(2499.99)
.category('Electronics')
.build();
console.log(laptop.getName());
console.log(laptop.toString());Typing generated APIs (autogen)
TypeScript does not infer methods added by decorators. Use either:
autogenDataBuilder(MyClass)when you use@Data()and@Builder()together (same typings asautogen(MyClass, { builder: true, data: true } as const)).autogen(MyClass, features as const)with flags that match your stack, for example{ builder: true },{ data: true },{ instanceToString: true },{ instanceEquals: true }(the last two avoid clashing withObject.prototype.toStringin the type map).
autogen(MyClass) with one argument still returns MyClass unchanged for typing: add the second argument when you want completions for generated members.
Advanced usage
import { Value, Builder, Getter, autogen } from '@bollo-aggrey/ts-autogen';
@Value()
@Builder()
class ImmutablePoint {
@Getter() x: number = 0;
@Getter() y: number = 0;
distanceToOrigin(): number {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
}
const Point = autogen(ImmutablePoint, {
builder: true,
instanceToString: true,
instanceEquals: true
} as const);
const point = Point.builder().x(3).y(4).build();
console.log(point.distanceToOrigin());📚 Complete Decorator Reference
@Getter and @Setter
Generate getter and setter methods for class properties.
class User {
@Getter() private _name: string = '';
@Setter() private _email: string = '';
}
const user = new User();
user.setEmail('[email protected]');
console.log(user.getEmail());@ToString
Generates a toString() method.
@ToString()
class Point {
x: number = 0;
y: number = 0;
}
const point = new Point();
console.log(point.toString());@Equals
Generates an equals() method (value objects often pair this with @ToString() or use @Value() which applies both).
@Equals()
class User {
id: number = 0;
name: string = '';
}
const user1 = new User();
user1.id = 1;
user1.name = 'Alice';
const user2 = new User();
user2.id = 1;
user2.name = 'Alice';
console.log(user1.equals(user2));@RequiredArgsConstructor
Generates a constructor with required properties.
@RequiredArgsConstructor()
class User {
@Getter() private readonly id: number;
@Getter() private readonly name: string;
private readonly optional?: string;
}
const user = new User(1, 'Alice');@Data()
Combines @Getter, @Setter, @ToString, and @Equals.
import { Data, Getter, Setter, autogen } from '@bollo-aggrey/ts-autogen';
@Data()
class User {
@Getter() @Setter() name: string = '';
@Getter() @Setter() email: string = '';
}
const UserModel = autogen(User, { data: true } as const);@Builder()
Generates a fluent builder.
import { Builder, Getter, autogen } from '@bollo-aggrey/ts-autogen';
@Builder()
class Config {
@Getter() host: string = '';
@Getter() port: number = 0;
}
const ConfigModel = autogen(Config, { builder: true } as const);
const config = ConfigModel.builder().host('localhost').port(3000).build();@AllArgsConstructor() / @NoArgsConstructor()
import { AllArgsConstructor, NoArgsConstructor, Getter, Builder, autogen } from '@bollo-aggrey/ts-autogen';
@AllArgsConstructor()
@NoArgsConstructor()
class Point {
@Getter() x: number = 0;
@Getter() y: number = 0;
}
const PointModel = autogen(Point, { builder: true } as const);
const point1 = new PointModel(10, 20);
const point2 = new PointModel();⚙️ Configuration
TypeScript Configuration
Add to your tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2020",
"module": "ESNext"
}
}Version Checking
The package includes an automatic version checker that runs on postinstall. To disable it, set the environment variable:
export TS_AUTOGEN_SKIP_VERSION_CHECK=trueOr check for updates manually:
npx ts-autogen-check --check-updates🎯 Why Use TypeScript Autogen?
- ✅ Reduced Boilerplate - Generate common methods automatically
- ✅ Type safety - Strong typings when you use
autogen/autogenDataBuilderwith a matching feature map - ✅ Familiar API - Similar to Java Lombok for easy adoption
- ✅ Runtime decorators - Generated members are attached when the class is defined (no separate build plugin required)
- ✅ Immutability Support - First-class support for immutable data structures
- ✅ Framework Agnostic - Works with any TypeScript project
- ✅ Automatic Updates - Built-in version checking to keep you up-to-date
📋 Requirements
- Node.js 20.0+
- TypeScript 4.5+
"experimentalDecorators": trueintsconfig.json"emitDecoratorMetadata": trueintsconfig.json(required for some features)
🧪 Testing
npm test🤝 Contributing
Contributions welcome! Open issues and PRs are appreciated.
📄 License
MIT License – see LICENSE
🙏 Acknowledgments
- Inspired by Project Lombok for Java
- Made with ❤️ for the TypeScript community
