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 languagests_quote
: Utilities for generating TypeScript code from Rust
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
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