facet-typescript 0.42.0

Generate TypeScript type definitions from facet type metadata
Documentation

facet-typescript

Coverage Status crates.io documentation MIT/Apache-2.0 licensed Discord

facet-typescript

Generate TypeScript type definitions from facet type metadata.

This crate uses facet's reflection capabilities to generate TypeScript interfaces and types from any type that implements Facet. Unlike going through JSON Schema, this generates TypeScript directly, preserving:

  • Exact optional field semantics
  • Union types for enums
  • Literal types for discriminated unions
  • Proper readonly modifiers

Usage

use facet::Facet;
use facet_typescript::to_typescript;

#[derive(Facet)]
struct User {
    name: String,
    age: u32,
    email: Option<String>,
}

let ts = to_typescript::<User>();
println!("{}", ts);

Output

export interface User {
  name: string;
  age: number;
  email?: string;
}

Multiple Types

Generate types for multiple related types at once:

use facet_typescript::TypeScriptGenerator;

let mut gen = TypeScriptGenerator::new();
gen.add_type::<User>();
gen.add_type::<Post>();
gen.add_type::<Comment>();

let ts = gen.finish();

LLM contribution policy

Sponsors

Thanks to all individual sponsors:

...along with corporate sponsors:

...without whom this work could not exist.

Special thanks

The facet logo was drawn by Misiasart.

License

Licensed under either of:

at your option.