Note
This project is still a work in progress.
This package help you create Rust flavored datatypes in TypeScript.
Note
Currently rusty-types is not published to JSR. You can import it with JSDelivr.
Two foundamental exports from rusty-types
is Struct
and Enum
.
rusty-types
use standard-schema
in its interface,
so you can use Zod, Valibot, Arktype
or any other validator to your best fits.
Case | Rust | rusty-types |
---|---|---|
Named struct |
struct Person {
name: String,
age: usize,
}
let person = Person {
name: "Alice".into(),
age: 20
}; |
const Person = Struct({
name: z.string(),
age: z.number(),
});
const alice = new Person({
name: "Alice",
age: 20
});
const bob = Person({
name: "Bob",
age: 22
}); |
Tuple struct |
struct Vec3(f32, f32, f32);
let vec = Vec3(1., 2., 3.); |
const Vec3 = Struct([
z.number(),
z.number(),
z.number(),
]);
const vec_1 = new Vec3(1, 2, 3);
const vec_2 = Vec3(4, 5, 6); |
Enum |
enum Event {
Key { code: usize },
Mouse {
state: usize,
button: usize,
},
Paste(String),
}
let event_1 = Event::Key {
code: 123
};
let event_2 = Event::Mouse {
button: 123,
state: 0,
};
let event_3 = Event::Paste(
"some text".into()
); |
const Event = Enum({
Key: { code: z.number() },
Mouse: {
state: z.number(),
button: z.number()
},
Paste: [z.string()] as const,
});
const event_1 = Event.Key({
code: 123
});
const event_2 = new Event.Mouse({
button: 123,
state: 0
});
const event_3 = new Event.Paste(
"some text"
); |
- custom transform
- methods
- extends
- auto JS-lize(e.g.
get_name()
->get name()
)