TypeScript
Generates TypeScript interfaces and optionally Zod validation schemas.
Type Mappings
| Rust Type | TypeScript Type |
|---|---|
String | string |
bool | boolean |
u8, u16, i8, i16 | number |
u32, i32, f32, f64 | number |
u64, i64, u128, i128 | bigint |
Uuid | string |
DateTime<Utc> | string |
Option<T> | T | undefined |
Vec<T> | T[] |
HashMap<K, V> | Record<K, V> |
Example
Rust:
#![allow(unused)]
fn main() {
#[derive(TypeWriter)]
#[sync_to(typescript)]
pub struct User {
pub id: String,
pub email: String,
pub age: Option<u32>,
}
}
Generated:
export interface User {
id: string;
email: string;
age?: number | undefined;
}
Zod Schemas
Zod schemas are generated by default alongside interfaces. They provide runtime validation:
import { z } from 'zod';
export const UserSchema = z.object({
"id": z.string(),
"email": z.string(),
"age": z.number().optional(),
});
Disable Zod schema generation:
[typescript]
zod = false
Or per-type:
#![allow(unused)]
fn main() {
#[derive(TypeWriter)]
#[sync_to(typescript)]
#[tw(zod = false)]
pub struct InternalOnly {
// ...
}
}
Readonly Fields
Make all fields readonly:
[typescript]
readonly = true
export interface User {
readonly id: string;
readonly email: string;
}
File Naming
Files use kebab-case by default: UserProfile → user-profile.ts
Change style in typewriter.toml:
[typescript]
file_style = "snake_case" # user_profile.ts
# or
file_style = "PascalCase" # UserProfile.ts