Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

TypeScript

Generates TypeScript interfaces and optionally Zod validation schemas.

Type Mappings

Rust TypeTypeScript Type
Stringstring
boolboolean
u8, u16, i8, i16number
u32, i32, f32, f64number
u64, i64, u128, i128bigint
Uuidstring
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: UserProfileuser-profile.ts

Change style in typewriter.toml:

[typescript]
file_style = "snake_case"  # user_profile.ts
# or
file_style = "PascalCase"  # UserProfile.ts