You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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*/exportinterfaceMyStruct{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
The text was updated successfully, but these errors were encountered:
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:
The issue is that unless we are using
#[serde(skip_serializing_if = "Option::is_none")]
attribute, then ifmy_name
has valueNone
it gets serialized asnull
in JSON:However, the generated Typescript definition does not reflect that
my_name
can hold anull
value: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
The text was updated successfully, but these errors were encountered: