Add ability to emit literal Typescript from with Go file#52
Merged
Conversation
added 3 commits
February 21, 2024 18:07
This solves the problem of a struct field being marshalled into a tuple. It could also be acheived by putting frontmatter into tygo.yaml, but that separates the TS definition from the type. Ideally we would like to say `tstype:"[a:number, b:number,c:string]"` in the field tag, but commas are inescapably reserved for tag delimiters.
This allows multi-line content more easily.
gzuidhof
approved these changes
Mar 4, 2024
gzuidhof
left a comment
Owner
There was a problem hiding this comment.
Looks great, thank you!
This is a great contribution :)
| return false | ||
| } | ||
| isEmit := false | ||
| if x.Tok == token.VAR { |
Owner
There was a problem hiding this comment.
Perhaps this makes sense to support token.CONST too? (or perhaps there is a good reason not to?)
Contributor
Author
There was a problem hiding this comment.
Consts are already handled to export the const definition itself, so I'm inclined to not handle them here as it makes it more complex it we have to decide which way to handle it.
Co-authored-by: Guido Zuidhof <me@guido.io>
Owner
|
Let me know when you are happy for me to merge this |
Contributor
Author
I'm happy if you are happy with not handling |
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.
This implements a feature to allow a Go file to contain Typescript definitions that can't be expressed through
tstypetags.There are actually two ways of doing it implemented. I think both have their uses but perhaps only one should be included, in which case I would say the var string literal one should be preferred.
Motivating Example:
We have a struct that is marshalled into JSON, and therefore exposed to TS, as a tuple of values, to minimise size on the wire. (it saves about 30% after gzip compression)
Prior art:
There are of course already ways to handle this.
frontmatterin the configuration, but I think this separates the TS declaration from the Go too much.I hope you find this change acceptable and thanks for reading.