Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
I have a project which is a rust component in a larger typescript codebase. We are using Tsify to create strongly typed bindings to our various methods. However, as we are dealing with similar concepts on both sides, we run into a lot of type name conflicts between exported-rust types and typescript native types. We want to prefix all of the types exported from our rust codebase so that it's clear that the given type is coming from the rust bundle. To facilitate this, without needing to prefix all of actual rust types, we have added two attributes to the tsify macro.
Attributes
The new attributes are
type_prefix
andtype_suffix
which work recursively.will generate:
We will put this attribute on all of our types and then all of our bindings will use custom types that are prefixed by the given prefix.
Alternatives
As derive macros can't really have global configuration, this seems like the best way to do this on a global scale.
We tried using
#[serde(rename = "...")]
combined with#[tsify(type = "...")]
but it becomes extremely tedious and error prone as soon as you have a lot of nested structs because thetype
annotation does no verification you didn't put the wrong type, and you need to spell out all the components of complex types.Comments
The code change is a little large as it passes the configuration through to formerly pure functions, but this perfectly sets us up for things like #26 and other type-gen options.
TODO
I haven't done this in case we want to discuss the design more, but once it's solidified, I'll add to the readme.