MCPcopy Index your code
hub / github.com/Wulf/tsync

github.com/Wulf/tsync @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
184 symbols 261 edges 72 files 34 documented · 18%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

tsync

License: MIT OR Apache-2.0

A utility to generate typescript types from rust code. Tries to conform to serde's serialized output.

Install

There are two parts to this:

  1. The CLI tool (or see "Usage as a library"):

cargo install tsync

  1. The dependency for rust projects (to use the #[tsync] attribute; see usage below)

cargo add tsync@1

Usage

Mark structs with #[tsync] as below:

/// src/main.rs
use tsync::tsync;

/// Doc comments are preserved too!
#[tsync]
struct Book {
  name: String,
  chapters: Vec<Chapter>,
  user_reviews: Option<Vec<String>>
}

#[tsync]
struct Chapter {
  title: String,
  pages: u32
}


#[tsync]
/// Time in UTC seconds
type UTC = usize;

Then use the CLI tool:

tsync -i ./src -o types.d.ts

And voilà!

/// types.d.ts

/* This file is generated and managed by tsync */

// Doc comments are preserved too!
interface Book {
  name: string
  chapters: Array<Chapter>
  user_reviews?: Array<string>
}

interface Chapter {
  title: string
  pages: number
}

// Time in UTC seconds
type UTC = number

Supported Conversions & Examples

Rust code with #[tsync] Typescript output
struct .d.ts file or .ts file
type .d.ts file or .ts file
enum .d.ts file or .ts file
const (or limited json! support) .ts file

Note: if you specify a .d.ts extension for your output, rust consts with #[tsync] are ignored |

Multiple Inputs

You can specify many inputs (directories and/or files) using the -i flag multiple times, like so:

tsync -i directory1 -i directory2 -o types.d.ts

Multiple Outputs

It might help to create multiple typing files for your project. It's easy, just call tsync multiple times:

tsync -i src/models -o models.d.ts
tsync -i src/api -o api.d.ts

Usage as a library

In the case that installing tsync globally isn't an option (or causes other concerns), you can use it as a library.

  1. Add the library to your project:

sh cargo add tsync@1

  1. Create a new binary in your project which uses the crate (for example, bin/tsync.rs):

```rust // bin/tsync.rs

use std::path::PathBuf;

pub fn main() { let dir = env!("CARGO_MANIFEST_DIR");

   let inputs = vec![PathBuf::from_iter([dir, "backend"])];
   let output = PathBuf::from_iter([dir, "frontend/src/types/rust.d.ts"]);

   tsync::generate_typescript_defs(inputs, output, false);

} ```

  1. Create a Cargo.toml binary entry:

toml [[bin]] name = "tsync" path = "bin/tsync.rs"

  1. Execute!

sh cargo run --bin tsync

Protip: to use cargo tsync, create an alias in .cargo/config:

toml [alias] tsync="run --bin tsync"

Errors

A list of files which can't be opened or parsed successfully are listed after executing tsync. For other errors, try using the --debug flag to pinpoint issues. Please use the Github issue tracker to report any issues.

Docs

See tsync --help for more information.

Feel free to open tickets for support or feature requests.

Development/Testing

Use ./test/test_all.sh to run tests. After running the test, there should be no unexpected changes to files in ./test (use git status and git diff to see if there were any changes).

License

This tool is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.

Extension points exported contracts — how you extend this code

ToTypescript (Interface)
(no doc) [4 implementers]
src/to_typescript/mod.rs
BookCamel (Interface)
Book struct with camelCase field names.
test/struct/typescript.d.ts
StructTest (Interface)
struct comment
test/doc_comments/typescript.d.ts
Book (Interface)
Doc comments are preserved too!
test/directory_input/typescript.d.ts
Book (Interface)
(no doc)
test.d.ts
Request (Interface)
(no doc)
test/enum_newtype/typescript.d.ts
SomeStruct (Interface)
(no doc)
test/issue-34/typescript.d.ts
Folder (Interface)
(no doc)
test/generic/typescript.d.ts

Core symbols most depended-on inside this repo

get_attribute_arg
called by 11
src/utils.rs
get_comments
called by 10
src/utils.rs
write_comments
called by 10
src/lib.rs
convert_type
called by 6
src/typescript.rs
extract_struct_generics
called by 4
src/utils.rs
convert_generic
called by 4
src/typescript.rs
process_fields
called by 4
src/to_typescript/structs.rs
process_tuple_fields
called by 4
src/to_typescript/structs.rs

Shape

Interface 56
Enum 43
Function 40
Class 39
Method 6

Languages

Rust62%
TypeScript38%

Modules by API surface

src/utils.rs16 symbols
src/lib.rs12 symbols
test/struct/rust.rs8 symbols
test/enum/rust.rs8 symbols
test/enum_newtype/rust.rs7 symbols
src/typescript.rs7 symbols
test/raw_identifiers/rust.rs6 symbols
test/generic/rust.rs6 symbols
src/to_typescript/enums.rs6 symbols
test/struct/typescript.ts5 symbols
test/struct/typescript.d.ts5 symbols
test/issue-60/typescript.ts4 symbols

For agents

$ claude mcp add tsync \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact