Skip to content

Add ability to emit literal Typescript from with Go file#52

Merged
gzuidhof merged 4 commits into
gzuidhof:mainfrom
pdhammond42:main
Mar 5, 2024
Merged

Add ability to emit literal Typescript from with Go file#52
gzuidhof merged 4 commits into
gzuidhof:mainfrom
pdhammond42:main

Conversation

@pdhammond42

Copy link
Copy Markdown
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.

@gzuidhof gzuidhof left a comment

Copy link
Copy Markdown
Owner

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 :)

Comment thread README.md Outdated
Comment thread tygo/package_generator.go
return false
}
isEmit := false
if x.Tok == token.VAR {

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread tygo/write_toplevel.go Outdated
Co-authored-by: Guido Zuidhof <me@guido.io>
@gzuidhof

gzuidhof commented Mar 4, 2024

Copy link
Copy Markdown
Owner

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

@pdhammond42

Copy link
Copy Markdown
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
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