tsify icon indicating copy to clipboard operation
tsify copied to clipboard

Support for Type Renaming

Open katherine-hough opened this issue 2 months ago • 2 comments

While attempting to upgrade from tsify 0.4.5 to 0.5.6, we discovered that the behavior of tsify has changed so that type-level serde renames no longer impact the name of the corresponding TypeScript type declaration. The change appears to be intentional to address issue 43.

Are there any existing mechanisms that can be used to reenable type-level renaming? If not, would it be possible to add an attribute which could be used to enable type-level renaming?

katherine-hough avatar Oct 30 '25 20:10 katherine-hough

Perhaps

#[derive(Debug, Clone, Serialize, Deserialize, Tsify)]
#[serde(rename = "foo")]
pub struct Foo {
  x: i32,
}

should turn into

export type foo = Foo;
export interface Foo {
    x: number;
}

Would that fix the issue? Of course it could introduce other name collisions if people aren't careful.

siefkenj avatar Nov 01 '25 15:11 siefkenj

Unfortunately, that wouldn't fix the issue. The main reason that we need to be able to rename the produced typescript type is that we have multiple rust structs with the same name and use the rename to avoid a collision. It's similar to this issue (https://github.com/siefkenj/tsify/issues/16) for tsify-next.

I was thinking you could add an attribute like #[tsify(enable_type_renaming)] and if that flag is set, tsify would use the container name instead of the ident name for the type declaration. My understanding is that this was the behavior prior to this change: https://github.com/madonoharu/tsify/commit/3e81856908e8149a856082d194a55ecb402ff7d8.

katherine-hough avatar Nov 01 '25 20:11 katherine-hough

I have this issue too, since I like to keep my internal types and corresponding Tsify types separate using a pattern like:

struct MyType {
    ...
}

#[derive(Tsify, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
struct TsMyType {
    ...
}

impl From<MyType> for TsMyType {
    ...
}

To prevent name collisions, all types that are Tsify use the Ts prefix, which I would like to be able to remove when the TS is generated.

jakemoran avatar Nov 29 '25 16:11 jakemoran