A lightweight TypeScript DSL for AST construction and code generation. Makes code generation readable, concise, and easy to write, reducing boilerplate compared to ts.factory
and improving performance compared to ts-morph
.
- Native TypeScript AST: Returns actual
ts.MethodDeclaration
,ts.ClassDeclaration
, etc. - Interoperable: Mix and match with
ts.factory
functions - Simple API: Clean, minimal interface with fluent chaining
- No Wrapper Types: Direct access to TypeScript AST nodes
- Minimal boilerplate: Flat API with concise helpers
- Readable & maintainable: Clearly shows structure and intent
- Full TypeScript API access: Use any
ts.factory
function alongside this DSL - Future proof: Compatible with TypeScript compiler updates
- Handles core codegen tasks: Classes, functions, methods, properties, imports/exports, file management
npm install ts-flattered typescript
import { cls, method, param, block, call, $, sourceFile, writeAll } from "ts-flattered";
sourceFile("Dog.ts", [
cls("Dog", [
method({
name: "bark",
body: block([call("console.log", [$("Woof!")])])
})
])
]);
await writeAll({ outputDir: "./generated" });
sourceFile(name, statements)
- Create a TypeScript fileklass(name, members?)
- Create a class with optional membersmethod(name, params?, body?)
- Create a method with optional params and bodyprop(name, type?)
- Create a property with optional typeparam(name, type?)
- Create a method parameterblock(statements)
- Create a code blockcall(functionName, args?)
- Create a function call$(value)
- Create a string/identifier literalassign(target, value)
- Create an assignment expressionwriteAll(opts)
- Generate all files to disk
Aspect | ts-flattered | ts-morph | ts.factory |
---|---|---|---|
Readability | 🟢 Excellent | 🟡 Good | 🔴 Poor |
Performance | 🟢 Excellent | 🔴 Poor | 🟢 Baseline |
Type Safety | 🟢 Excellent | 🟡 Good | 🟢 Excellent |
Learning Curve | 🟢 Excellent | 🟡 Good | 🔴 Poor |
Bundle Size | 🟢 Excellent | 🟡 Good | 🟢 Excellent |
Code Generation | 🟢 Excellent | 🟡 Good | 🟡 Good |
Key Benefits:
- 90% less code than
ts.factory
with better readability - 40-45x faster than
ts-morph
for generation tasks - Native TypeScript AST - no wrapper types
- Perfect for: Code generation, DSLs, build tools, metaprogramming
ts-flattered offers excellent performance with minimal overhead:
- 1.6x slower than raw
ts.factory
(declarative style) - 1.8x slower than raw
ts.factory
(chainable style) - 40-45x faster than
ts-morph
MIT