Generates TypeScript serialisation and deserialisation code from Rust structs and enums
- Generate TypeScript code directly from Rust source
- TypeScript must compile with
strict
mode enabled - Avoid Object Orientated TypeScript for better tree-shaking and optimisation
- TypeScript should be ergonomic and high performance
- Use
const enum
for Unit enums and respect discriminant values! - Use
TypedArray
and copy byte blocks forVec<{integer,float}>
for greater performance
- Use
I'm pretty new to Rust and I've just hacked around until the tests pass 🤷♂️
There is much room for improvement and PRs are welcome!
Check the source for currently supported Rust types and their TypeScript equivalents.
You may also like to look at the Rust types used in the tests and the TypeScript generated from these.
- All values must be owned
- Generic structs/enums will almost certainly cause a panic
- All types must be in a single file
- Serde attributes are not currently respected
Vec<T>
are always converted toUint8Array/Int8Array/etc
whenever possible and this might not always be desired.- Generated code will not work on node < v11 due to the global usage of
TextEncoder/TextDecoder
There is currently a single bool
option to enable support for node.js
Buffer
, so if you are running in the browser you probably don't want this enabled.
bincode_typescript::from_file("./src/types.rs", "./ts/types.ts", false);
There is currently a single option (--buffer-support
) to enable support for node.js
Buffer
.
./bincode-typescript --support-buffer ./src/types.rs > ./ts/types.ts
Before running the tests, ensure that you have all the node.js dependencies
installed by running yarn
or npm i
.
The tests check serialisation and deserialisation from generated TypeScript by round-tripping encoded data via stdio and asserting the expected values.
This builds on (ie. much TypeScript stolen from) the following projects.
The stated pros and cons are just personal opinion!
- Function based TypeScript API
- Great ergonomics for enums with combination of
type
+interface
+module
- Generates both Rust and TypeScript from a DSL. (I want Rust to be the source of truth).
- Does not use
const enum
for Unit enums
- Uses
serde
so no messing around parsing Rust
- All types have to be run through the registry after build so wont work from
build.rs
- TypeScript classes wrap every type and use inheritance (ie. no
const enum
) - Runtime TypeScript is separate