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

Typescript: Option<> does not include null in generated types #244

Open
maiia-kuzmishyna opened this issue Apr 7, 2025 · 0 comments
Open

Comments

@maiia-kuzmishyna
Copy link

Description

We are using typeshare to auto-translate Rust types into Typescript ones and facing issues with Option<> type.
Assume we have the following type:

#[typeshare]
struct MyStruct {
    my_name: Option<String>,
    my_age: u32,
}

The issue is that unless we are using #[serde(skip_serializing_if = "Option::is_none")] attribute, then if my_name has value None it gets serialized as null in JSON:

{
  "my_name": null,
  "my_age": 21
}

However, the generated Typescript definition does not reflect that my_name can hold a null value:

/*
 Generated by typeshare 1.13.2
*/

export interface MyStruct {
  my_name?: string;
  my_age: number;
}

This leads to potential unchecked nulls in our typescript part of the project.

Proposed solution

The best solution would be to adapt how Option<> types are treated based on the presence or absence of serde attribute.
If this is not possible, then null could simply be added to type definition of optional properties, enforcing safer typechecking.
However, that might be a breaking change to projects already relying on serde attribute so perhaps there should be a command line option to opt-in to this behavior.

Related issues

There is a similar issue about unit type: #43 which has been attempted to be fixed by #82 however reverted due to #96 and now is hanging in a draft pr #98.

Version

typeshare-cli version 1.13.2
typeshare crate version 1.0.4

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

1 participant