Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bes opened this issue Feb 6, 2024 · 3 comments
Open

Comments

@bes
Copy link

bes commented Feb 6, 2024

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
Copy link

siefkenj commented Feb 6, 2024

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

@bes
Copy link
Author

bes commented Feb 7, 2024

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants