ts-types Better Support for Types useful for Functions#1
Open
logankaser wants to merge 3 commits intoryangoree:mainfrom
Open
ts-types Better Support for Types useful for Functions#1logankaser wants to merge 3 commits intoryangoree:mainfrom
logankaser wants to merge 3 commits intoryangoree:mainfrom
Conversation
Author
|
I ended up including the updated version of ts-types in https://crates.io/crates/ts-function, but it would be great to switch back to the official version in the future. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Background
It all started when I decided a good DX for my embedding my Rust app inside an existing React app, was to follow common
JS patterns, and have my Rust app take an object of callback functions at startup. They led me down quite the rabbit hole.
Upon discovering that
tsifydespite the excellent name, is sadly Serde-based and therefore uses JSON for FFI (gross)and does not support
Functionas a result, I was sad.Then, I discovered your library
ts_macrowhich does supportFunction. Reading your code, I noticed that for unhandled types, it just used the name of the type in TypeScript.Presumably so that if someone uses
typescript_custom_section, it will do the right thing.This meant if I could make a thing that turned function types in Rust into TypeScript types, I would be able to use
ts-macroto allow my module to take a struct of typed callbacks at startup. Which is a good pattern for Rust >JS communication, especially for my application, which runs / loops inrequestAnimationFrame, so just return values from functions are not really enough.While adapting
ts-typesto theFunctiontypes use case, I had to make a few changes to better support function types, and also for performance reasons.Here's what I did:
Option,Vec, and other containers. This allows for reliable mapping of deeply nested types (e.g.,Option<Vec<String>>).This allowed me to drop the deps on
regexandlazy_staticentirely.Result<String, i32>to correctly outputResult<string, number>. This is a lot more important for functions than it is for structs, as you might imagine, as result types are pretty important there.Arc<T>,Rc<T>, andBox<T>as transparent wrappers, ensuring the TypeScript definitions focus on the underlying data. This is really only useful for when you're wrapping a Rust struct that might not have been made for thetsmacro and also makes that macro and my function macro a little more flexible.u8orf64) now automatically map to their high-performance JavaScript equivalents, such asUint8ArrayorFloat64Array. This avoids expensive element-by-element copying ofnumber[]. Probably more significant for functions than for structs most of the time. This also matches the behavior ofwasm-bindgenitself for functions, which I think is more intuitive.strip_typehelper was stringifying the Rust slice type&[u8]as the string"[u8]". Because that string uses square brackets, the TypeScript parser (correctly) identified it as a TypeScript tuple of length 1, rather than a TypeScript array.The helper now produces
"u8[]"instead of"[u8]", so it correctly parses as an array.Also, I updated
TsType::try_fromto handleType::SliceandVec<T>directly at the AST level. It now explicitly constructs aTsType::Array(which correctly renders as T[]), bypassing the string parser entirely for these common types.As a side effect,
Vec<Vec<f64>>now correctly maps toFloat64Array[](an array ofFloat64Array)