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

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

Open
lukaszlew opened this issue Jun 12, 2023 · 6 comments
Open

Comments

@lukaszlew
Copy link

E.g.

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

would not add Uuid itself to type-shared types.

Please advise.

@czocher
Copy link
Contributor

czocher commented Jun 18, 2023

@lukaszlew

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

results in:

export type Id = string;

If you generate TypeScript out of it.

@lukaszlew
Copy link
Author

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

@LouisGariepy
Copy link

LouisGariepy commented Jun 27, 2023

@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
Copy link
Contributor

czocher commented Jun 29, 2023

@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
Copy link
Author

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
Copy link
Contributor

czocher commented Sep 19, 2023

@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

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

3 participants