Skip to content

spencerkohan/type_reflect

Repository files navigation

Type Reflect Monorepo

This is the monorepo for type_reflect and ts_quote. It contains two main projects:

  • type_reflect: Runtime reflection for Rust types, to facilitate easy bridging between Rust types and other languages
  • ts_quote: Utilities for generating TypeScript code from Rust

1. 🪩 Type Reflect

type_reflect provides procedural macros to allow for runtime reflection on Rust types. It's main goal is to facilitate sharing serializable types between languages, for example in the use-case of sharing types between a Rust webservice, consumed by a TypeScript client.

It provides some utilities out of the box for exporting Rust types to TypeScript, both as raw TS types and Zod schemas, and is designed to be extensible, so the user can implement custom type exporters to meet thier own specific use-case.

📝 Example usage

Give types runtime reflection using the Reflect derive macro:

#[derive(Reflect)]
struct Message {
    index: u32,
    text: Option<String>,
}

Export types using the export_types! macro:

export_types!(
    types: [
        Message
    ]
    exports: [
        Zod("/path/to/zod_export.ts"),
        TypeScript("/path/to/ts_export.ts", tab_width: 2),
    ]
)

Invoking this macro will generate the following ts_export.ts file:

export type Message = {
index: number;
text?: string;
};

and the following zod_export.ts:

import { z } from 'zod';

export const MessageSchema = z.object({
    index: z.number(),
    text: z.string().optional(),
});

export type Message = z.infer<typeof MessageSchema>;

For more examples check the type_reflect crate README

📦 Type Reflect Crates:

Crate Description Links
type_reflect The main type_reflect crate for public consumption. Github Crates.io Documentation
type_reflect_macros Procedural macro implementations for type_reflect. This crate is for internal use, and the macros are re-exported by the type_reflect crate. Github Crates.io Documentation
type_reflect_core A crate for shared components used by both type_reflect and type_reflect_macros. Github Crates.io Documentation

2. 🖊️ TS Quote

ts_quote provides procedural macros and utilities for generating TypeScript code in Rust.

Usage is similar to the popular quote crate for Rust code generation.

📝 Example usage

Create a TypeScript string using the ts_string! macro:

let ts: String = ts_string!{ const foo: number = 1; };

Embed Rust runtime values in the output by prefixing with #:

let var_name = "foo";
let value = 1;

let ts: String = ts_string!{ const #var_name: number = #value; };
// the value of ts is "const foo: number = 1;"

Output pretty-printed TypeScript:

let ts_func: TS = ts_quote! {
    const add = (x: number, y: number) => {
        return x + y;
    };
}?;

let pretty: String = ts_func.formatted(None)?;

For more examples check the ts_quote crate README

📦 TS Quote Crates:

Crate Description Links
ts_quote The main ts_quote crate for public consumption. Github Crates.io Documentation
ts_quote_macros Procedural macro implementations for ts_quote. This crate is for internal use, and the macros are re-exported by the ts_quote crate. Github Crates.io Documentation

About

A library for making it easy to share types between Rust and TypeScript

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages