-
Notifications
You must be signed in to change notification settings - Fork 13
Description
I run into an issue regarding the types generated for structs with flattened enums.
For example when I define:
#[derive(Tsify)]
pub struct Root {
pub id: usize,
pub name: String,
#[serde(flatten)]
pub root_type: RootType,
}
#[derive(Tsify)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum RootType {
Type1,
Type2 {
extra: bool,
},
}the following TS gets generated:
export type RootType = { type: "type1" } | { type: "type2"; extra: boolean };
export interface Root extends RootType {
id: number;
name: string;
}The problem lies in the interface Root extends RootType statement which results in the following error being emitted by tsc:
An interface can only extend an object type or intersection of object types with statically known members.(2312)
and at least in vscode intellisense doesn't pickup the type correctly.
I couldn't find any macro attributes that would result in the correct TS output but maybe I'm missing something. Other than manually changing the TS Root definition to use type instead of interface a workaround is to change the Rust Root to an enum:
#[derive(Tsify)]
#[serde(untagged)]
pub enum Root {
Root {
id: usize,
name: String,
#[serde(flatten)]
root_type: RootType,
}
}which yields a working TS definition, albeit at the expense of the Rust side ergonomics:
export type Root = { id: number; name: string } & RootType;I get the same behavior with both 0.5.4 and 27d6982.
If this is truly a bug and not a user error I can try to have a peek under the hood at some point, but that being said I don't have a ton of experience with Rust macros so it will likely take some time 😀