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

Allow for exporting constants #95

Closed
Isaac-Leonard opened this issue May 23, 2024 · 3 comments
Closed

Allow for exporting constants #95

Isaac-Leonard opened this issue May 23, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@Isaac-Leonard
Copy link

Would it be possible to add a way to export constants as well as types?
This would be quite useful when using static data on both the backend and frontend such as names of things without having to define them seperately and risk bugs from mispelling things such as arrays of enum names generated from the strum crate or list of driver names from gdal.
It would probably be simple enough to do this manually however integrating with existing code generation would be cleaner and allow for configuring it all in one place.

@oscartbeaumont oscartbeaumont added the enhancement New feature or request label May 23, 2024
@oscartbeaumont
Copy link
Member

This will be supported in the next release using the following syntax:

let builder = ts::builder()
    // < your commands and events are probaly here
    .types(StaticCollection::default().register("myConstant", 42)); // < call `register` as much as you want.

It requires that the value (42 in the example above) implements serde::Serialize and it puts it into the Tauri Specta bindings like the following:

export const myConstant = 42 as const; // `as const` will be applied Tauri Specta detects it's valid.

Let me know how this goes for your usecase as I would be open to refining the feature if you run into any issues!

@kareemmahlees
Copy link
Contributor

Just for future reference, the example mentioned here and in the readme are wrong, it should be:

let builder = ts::builder()
    // < your commands and events are probaly here
    .statics(StaticCollection::default().register("myConstant", 42)); // < call `register` as much as you want.

@oscartbeaumont
Copy link
Member

Fixed. Thanks for pointing it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants