Aleph-Alpha / ts-rs

Generate TypeScript bindings from Rust types

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: Typed array types

eye-wave opened this issue · comments

Is your feature request related to a problem? Please describe.
It would be to have Integer of Float Vectors be converted to typed arrays.

example

Vec<f32> -> Float32Array
Vec<u8> -> Uint8Array
Vec<i32> -> Int32Array 

Describe alternatives you've considered
Tsify, but it only works with wasm-bindgen which i don't need for a tauri app

adding #[ts(type = "MyArray")] for each problematic property

I'm not sure we can implement this at all.

At the moment, Vec<T> simply exports Array<T>, which is generally what the user expects, even in Tauri, as most of the time the serialization is done through serde.

We do this through a generic impl block on Vec that goes something like

impl<T: TS> TS for Vec<T> {
    // ... export T, but wrapped in Array<>
}

So we can't just write

impl TS for Vec<u8> {
    // Export as Uint8Array
}

because that would generate a compiler error complaining about conflicting trait impls.

Also, most people don't expect an Uint8Array, so even if it were allowed, we still couldn't do that easily, as it can't be the default behavior.

I do recommend that you create a type that exports as Uint8Array and use #[ts(as = "...")] instead of #[ts(type = "...")] though, as it helps avoid typos

@eye-wave I'd be interested in how you do (de)serialization such that you'd profit from ts-rs generating typed arrays. What format are you using for (de)serialization, and which libraries do you use for them on the Rust/TS side?

I'd generally be open to support more data models besides the serde data model, but the serde model is the only commonly used one i am aware of.

I'm 90% sure that it works like that with binaries generated with wasm-bindgen