Here’s a clean, professional README.md tailored for your Dart → TypeScript (Zod) generator package:
Generate TypeScript types and Zod schemas automatically from your Dart models using build_runner.
Perfect for sharing models between Flutter (Dart) and backend/frontend TypeScript projects.
-
🔄 Convert Dart models → TypeScript interfaces
-
🛡 Generate fully-typed Zod validation schemas
-
📦 Supports
@JsonSerializablemodels -
🧠 Detects manual
fromJson/toMappatterns automatically -
🔗 Cross-file type resolution (imports handled for you)
-
🧬 Enum →
z.enum()conversion -
🧩 Handles:
- Nested models
- Lists & Maps
- Nullable fields
- Default values
- Inheritance
-
🔁 Circular dependency safe
-
⚙️ Configurable (
DateTimehandling, etc.)
lib/src/User.dart → gen/src/User.g.ts
lib/src/enums/Role.dart → gen/src/enums/Role.g.tsThen:
gen/**/*.g.ts → compiled → dist/Add to your dev_dependencies:
dev_dependencies:
dart_ts_generator:
git:
url: https://gitlab.com/ucmonks/eimodels.gitAlso required:
dependencies:
build_runner: ^2.4.0Create or update build.yaml:
targets:
$default:
builders:
dart_ts_generator:
options:
date_time_as_string: true| Option | Description | Default |
|---|---|---|
date_time_as_string |
Convert DateTime → string instead of Date |
true |
Run the generator:
dart run build_runner buildOr watch mode:
dart run build_runner watch@JsonSerializable()
class User {
final String id;
final String? name;
User({required this.id, this.name});
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}class Product {
final int id;
Product(this.id);
static Product fromJson(Map<String, dynamic> json) {
return Product(json['id']);
}
Map<String, dynamic> toMap() {
return {'id': id};
}
}enum Status { pending, completed, failed }// ── Status (Enum) ──
export const StatusSchema = z.enum(['pending', 'completed', 'failed']);
export type Status = z.infer<typeof StatusSchema>;
// ── User (Type) ──
export interface User {
id: string;
name?: string | null;
}
// ── User (Schema) ──
export const UserSchema = z.object({
id: z.string(),
name: z.string().nullish(),
});Ignore a field:
@TsIgnore()
String internalField;Override TypeScript type:
@TsType('string | number', zodSchema: 'z.union([z.string(), z.number()])')
dynamic value;Force generation:
@TsGenerate()
class CustomModel {}Adds Firestore-specific handling (e.g. Timestamp).
| Dart | TypeScript | Zod |
|---|---|---|
| String | string | z.string() |
| int/double | number | z.number() |
| bool | boolean | z.boolean() |
| DateTime | string/Date | z.string()/z.date() |
| List | T[] | z.array() |
| Map | Record | z.record() |
Imports are automatically resolved:
import { RoleSchema } from '../enums/Role';No manual work needed.
-
.g.dartand.freezed.dartfiles are ignored -
Only relevant classes are generated:
@JsonSerializable- Manual serialization (
fromJson+toMap) @TsGenerate
-
Part files are skipped automatically
Run tests:
dart testMIT License
- Better Firestore support
- Custom transformers
- CLI mode (without build_runner)
- Swagger/OpenAPI generation