1Password / typeshare

Typeshare is the ultimate tool for synchronizing your type definitions between Rust and other languages for seamless FFI.
Apache License 2.0
2.34k stars 93 forks source link

How can I `typeshare` a type that has a field that is not under my control? #124

Open lukaszlew opened 1 year ago

lukaszlew commented 1 year ago

E.g.

#[typeshare]
struct Id(uuid::Uuid);

would not add Uuid itself to type-shared types.

Please advise.

czocher commented 1 year ago

@lukaszlew

#[typeshare]
struct Id(#[typeshare(serialized_as = "string")] uuid::Uuid);

results in:

export type Id = string;

If you generate TypeScript out of it.

lukaszlew commented 1 year ago

Excellent! Thank you. Consider adding it to the documentation.

LouisGariepy commented 1 year ago

@lukaszlew It is documented here.

But tbh it's definitely jarring that these docs are not really mentioned anywhere (at least as far as I know). Also, it seems like it uses mdbook, but the docs README doesn't really explain that fact or how to render the book locally.

I think it would be an important addition to increase the visibility of these docs. Perhaps even linking the relevant book docs in the docs.rs crate documentation (which are incredibly sparse). At least in the main README.

czocher commented 1 year ago

@LouisGariepy I agree that the docs require a bit of a makeover. The TypeScript readonly attribute is also not mentioned at all AFAIK. There's probably a lot of other things missing too.

lukaszlew commented 10 months ago

So now I know how to generate annotations for types of fields that I don't control

#[typeshare]
struct Id(#[typeshare(serialized_as = "string")] uuid::Uuid);

How about if it is the wrapping struct that I don't control? E.g.:

#[typeshare]
type = Result<String, String>

won't export the Result itself.

czocher commented 10 months ago

@lukaszlew I believe you can use the newtype idiom, see an example in the tests: https://github.com/1Password/typeshare/tree/main/core/data/tests/test_optional_type_alias

and the idiom documentation: https://doc.rust-lang.org/rust-by-example/generics/new_types.html