ts-rs icon indicating copy to clipboard operation
ts-rs copied to clipboard

Feature request: Typed array types

Open eye-wave opened this issue 1 year ago • 3 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

eye-wave avatar May 09 '24 09:05 eye-wave

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

escritorio-gustavo avatar May 09 '24 18:05 escritorio-gustavo

@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.

NyxCode avatar Jun 21 '24 00:06 NyxCode

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

eye-wave avatar Jun 21 '24 06:06 eye-wave