madonoharu / tsify

A library for generating TypeScript definitions from rust code.
Apache License 2.0
301 stars 41 forks source link

Tsify doesn't generate typescript definitions under certain conditions #50

Open bes opened 7 months ago

bes commented 7 months ago

Bug

Sometimes tsify won't generate a TypeScript interface.

How to reproduce

Specifically, under these circumstances:

Given a crate that has exactly one type that has a Tsify derive, and into_wasm_abi but NOT from_wasm_abi:

crates/somecrate/lib.rs

#[derive(Clone, Debug)]
#[derive(tsify::Tsify, serde::Serialize)]
#[tsify(into_wasm_abi)]
pub struct ExampleType {
    pub name: Option<String>,
}

AND the type does not appear in a direct return value (but it does appear in an indirect return value)

my_wasm_lib/lib.rs

#[wasm_bindgen(js_class = ExampleTypeHandleJs)]
impl ExampleTypeHandle {
    #[wasm_bindgen(js_name = getExampleTypeVec)]
    pub fn get_example_type_vec(&self) -> Option<ExampleTypeVec> {
        todo!()
    }
}

#[derive(Tsify, Serialize)]
#[tsify(into_wasm_abi)]
pub struct ExampleTypeVec(pub Vec<ExampleType>);

Then there will be no interface ExampleType in the resulting xxx.d.ts file.

Mitigation

However, this can be mitigated by one or more of the following:

Let the type appear as a direct return type

    #[wasm_bindgen(js_name = getExampleType)]
    pub fn get_example_type(&self) -> ExampleType {
        todo!()
    }

This is solution is unfortunate, because I have no use for such a method.

Let the type use from_wasm_abi

#[derive(Clone, Debug)]
#[derive(tsify::Tsify, serde::Serialize, serde::Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct ExampleType {
    pub name: Option<String>,
}

This solution is unfortunate, because I have no use for from_wasm_abi and serde::Deserialize which just add bloat.

@siefkenj I got a tip from @cwfitzgerald that you have a new fork with recent fixes, would you be open to enabling issues on your fork to get stuffed fixed there?

siefkenj commented 7 months ago

@bes Yes, I would be open to a PR on https://github.com/siefkenj/tsify Please make your PR to the next branch.

bes commented 7 months ago

Curiously, there is also a third mitigation:

Mitigation

Compile the wasm-library with codegen-units = 1

When compiling in release mode, the bug does not appear, which made me interested to find out why. I tried compiling dev mode with codegen-units = 1 which also made the TypeScript interface show up.

CinchBlue commented 1 month ago

I believe we just encountered this -- seems to still be present.