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 ability to emit literal Typescript from with Go file #52

Merged
merged 4 commits into from
Mar 5, 2024

Conversation

pdhammond42
Copy link
Contributor

This implements a feature to allow a Go file to contain Typescript definitions that can't be expressed through tstype tags.

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.

  • we could use frontmatter in the configuration, but I think this separates the TS declaration from the Go too much.
  • we could simply hand-craft the relevant TS and refer to it in an import in frontmatter, but that's an even bigger separation.
  • We could put comments into the Go that describe the mapping, which keeps it close but relies on the client reading and then using them.

I hope you find this change acceptable and thanks for reading.

Pete Hammond 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.
Copy link
Owner

@gzuidhof gzuidhof left a comment

Choose a reason for hiding this comment

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

Looks great, thank you!

This is a great contribution :)

return false
}
isEmit := false
if x.Tok == token.VAR {
Copy link
Owner

Choose a reason for hiding this comment

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

Perhaps this makes sense to support token.CONST too? (or perhaps there is a good reason not to?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

@gzuidhof
Copy link
Owner

gzuidhof commented Mar 4, 2024

Let me know when you are happy for me to merge this

@pdhammond42
Copy link
Contributor Author

Let me know when you are happy for me to merge this

I'm happy if you are happy with not handling const.

@gzuidhof gzuidhof merged commit 0287f98 into gzuidhof:main Mar 5, 2024
1 check passed
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.

2 participants