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

Add type_as attribute to specta #91

Merged
merged 4 commits into from Nov 30, 2022

Conversation

mablin7
Copy link
Contributor

@mablin7 mablin7 commented Nov 28, 2022

We were looking into using specta in our project, but ran into the issue of needing an escape hatch for certain types that can't (easily) implement Type. This solution is similar to ts_rs's #[ts(type = "..")] attribute.

#[specta(type_as = <Ident>)] on a field overrides its type in the generated type definition with the type of the identifier.

Example:

use specta::{ts::ts_export, Type};

pub enum MyEnum {
    A,
    B,
    C,
}

#[derive(Type)]
pub struct MyCustomType {
    #[specta(type_as=String)]
    pub nested: MyEnum,
}

fn main() {
    println!("{}", ts_export::<MyCustomType>().unwrap(),);
}

Any feedback is super appreciated!

`type_as = <Ident>` attribute overrides the type of the field in the
generated type definition.
Copy link
Collaborator

@Brendonovich Brendonovich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! Super cool idea to override the Rust type that is used rather than writing custom typescript.
I'd like the feature to mirror ts-rs a bit more, but you've already done quite a good job.
I might rebase this PR onto a bunch of new changes to Specta that are being made in preparation to make it more of a standalone library, I'll handle that though.

crates/specta/tests/ts_rs/field_override.rs Outdated Show resolved Hide resolved
crates/specta/examples/type_as.rs Outdated Show resolved Hide resolved
crates/specta/macros/src/type/attr/field.rs Outdated Show resolved Hide resolved
@Brendonovich
Copy link
Collaborator

Also would you mind explaining why you need type overrides, and why you picked Specta over ts-rs? Just curious haha

@Brendonovich Brendonovich changed the base branch from main to rework-type November 30, 2022 09:36
Copy link
Owner

@oscartbeaumont oscartbeaumont left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your time. Looks great!

@oscartbeaumont oscartbeaumont merged commit db6460f into oscartbeaumont:rework-type Nov 30, 2022
@mablin7
Copy link
Contributor Author

mablin7 commented Nov 30, 2022

Also would you mind explaining why you need type overrides, and why you picked Specta over ts-rs? Just curious haha

Oh, sorry I didn't have the time to address your comments, thanks for the corrections.

Our main problem was that the messages in our tauri app included types from 3rd party crates that we normally just serialize to string, but we can't have them implement Type.

The reason we would like to use specta is its ability to generate all referenced type definitions recursively. With ts-rs we have a huge list of types that need to be printed, and it needs to be updated manually. Whereas with specta, it would just be a single call, since we already have all relevant types in an enum anyway.

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

Successfully merging this pull request may close these issues.

None yet

3 participants