gzuidhof / tygo

Generate Typescript types from Golang source code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeScript enum support

klvnptr opened this issue · comments

Hello. Awesome package :) Quick question. Are you planning to support TypeScript enums? These can be transformed to TS enums.

type UserRole = string
const (
	UserRoleDefault UserRole = "viewer"
	UserRoleEditor  UserRole = "editor" // Line comments are also kept
)

Thanks
Peter

Hi Peter,

I believe that Typescript enums are never really a good idea to use, they are one of the only things in Typescript that have no direct translation to Javascript (so there ends up being extra JS output just for them).

It would be great if tygo could generate a union of literals though, so something like type UserRole = "viewer" | "editor", but in order to do so some changes are required in Tygo. It currently translates pretty much 1-to-1, where with this feature it would need to gather the cases and then output it (so it must have some sort of internal representation).

@klvnptr typescriptify-golang-structs has a way of working with structs (https://github.com/tkrajina/typescriptify-golang-structs#enums-with-tsname). You still have to manually list the struct elements, but converting to models ant typescript enums will work fine.

PS. I agree that structs are awkward if you make a Typescript library which is consumed from javascript. But if your project is 100% Typescript -- I see no reason not using them. It's easier to read enum references than string literals.

PPS. Good job with tygo!