@@ -53,7 +53,7 @@ type ListUsersResponse struct {
5353}
5454```
5555
56- _ Typescript output _
56+ _ Typescript output (with default ` enum_style: "const" ` ) _
5757
5858``` typescript
5959/**
@@ -141,7 +141,7 @@ err := gen.Generate()
141141# You can specify default mappings that will apply to all packages.
142142type_mappings :
143143 time.Time : " string /* RFC3339 */"
144-
144+
145145# You can specify more than one package
146146packages :
147147 # The package path just like you would import it in Go
@@ -173,6 +173,12 @@ packages:
173173 # Package that the generates Typescript types should extend. This is useful when
174174 # attaching your types to a generic ORM.
175175 extends : " SomeType"
176+
177+ # Enum generation style. Supported values: "const" (default), "enum", "union".
178+ # "const" generates individual export const declarations (traditional behavior).
179+ # "enum" generates TypeScript enum declarations for Go const groups.
180+ # "union" generates TypeScript union type declarations for Go const groups.
181+ enum_style : " enum"
176182` ` `
177183
178184See also the source file [tygo/config.go](./tygo/config.go).
@@ -207,10 +213,11 @@ You could use the `frontmatter` field in the config to inject `export type Genre
207213
208214** ` tygo:emit ` directive**
209215
210- Another way to generate types that cannot be directly represented in Go is to use a ` //tygo:emit ` directive to
216+ Another way to generate types that cannot be directly represented in Go is to use a ` //tygo:emit ` directive to
211217directly emit literal TS code.
212- The directive can be used in two ways. A ` tygo:emit ` directive on a struct will emit the remainder of the directive
218+ The directive can be used in two ways. A ` tygo:emit ` directive on a struct will emit the remainder of the directive
213219text before the struct.
220+
214221``` golang
215222// Golang input
216223
@@ -222,7 +229,7 @@ type Book struct {
222229```
223230
224231``` typescript
225- export type Genre = " novel" | " crime" | " fantasy"
232+ export type Genre = " novel" | " crime" | " fantasy" ;
226233
227234export interface Book {
228235 title: string ;
@@ -231,11 +238,12 @@ export interface Book {
231238```
232239
233240A ` //tygo:emit ` directive on a string var will emit the contents of the var, useful for multi-line content.
241+
234242``` golang
235243// tygo:emit
236244var _ = ` export type StructAsTuple=[
237- a:number,
238- b:number,
245+ a:number,
246+ b:number,
239247 c:string,
240248]
241249`
@@ -245,16 +253,11 @@ type CustomMarshalled struct {
245253```
246254
247255``` typescript
248- export type StructAsTuple = [
249- a :number ,
250- b :number ,
251- c :string ,
252- ]
256+ export type StructAsTuple = [a : number , b : number , c : string ];
253257
254258export interface CustomMarshalled {
255259 content: StructAsTuple [];
256260}
257-
258261```
259262
260263Generating types this way is particularly useful for tuple types, because a comma cannot be used in the ` tstype ` tag.
@@ -396,6 +399,111 @@ export interface ABCD<
396399}
397400```
398401
402+ ## TypeScript Enum and Union Generation
403+
404+ Tygo can generate native TypeScript enums or union types from Go const groups. When ` enum_style: "enum" ` is configured, tygo detects Go constant groups that follow enum-like patterns and converts them to TypeScript enums. When ` enum_style: "union" ` is configured, the same const groups are converted to TypeScript union types instead.
405+
406+ ### Requirements for Enum/Union Generation
407+
408+ For a const group to be recognized as an enum or union:
409+
410+ 1 . It must contain at least 2 exported constants
411+ 2 . All constants should share the same type (e.g., ` UserRole ` )
412+ 3 . Constant names should follow a consistent prefix pattern (e.g., ` UserRoleDefault ` , ` UserRoleEditor ` )
413+
414+ ### Examples
415+
416+ ** String Enums:**
417+
418+ ``` go
419+ // Go input
420+ type Status = string
421+ const (
422+ StatusActive Status = " active"
423+ StatusInactive Status = " inactive"
424+ StatusPending Status = " pending"
425+ )
426+ ```
427+
428+ ``` typescript
429+ // TypeScript output (with enum_style: "enum")
430+ export enum Status {
431+ Active = " active" ,
432+ Inactive = " inactive" ,
433+ Pending = " pending" ,
434+ }
435+ ```
436+
437+ ``` typescript
438+ // TypeScript output (with enum_style: "union")
439+ export const StatusActive = " active" ;
440+ export const StatusInactive = " inactive" ;
441+ export const StatusPending = " pending" ;
442+ export type Status = typeof StatusActive | typeof StatusInactive | typeof StatusPending ;
443+ ```
444+
445+ ** Numeric Enums with iota:**
446+
447+ ``` go
448+ // Go input
449+ type Priority int
450+ const (
451+ PriorityLow Priority = iota
452+ PriorityMedium
453+ PriorityHigh
454+ )
455+ ```
456+
457+ ``` typescript
458+ // TypeScript output (with enum_style: "enum")
459+ export enum Priority {
460+ Low = 0 ,
461+ Medium ,
462+ High ,
463+ }
464+ ```
465+
466+ ``` typescript
467+ // TypeScript output (with enum_style: "union")
468+ export const PriorityLow = 0 ;
469+ export const PriorityMedium = 1 ;
470+ export const PriorityHigh = 2 ;
471+ export type Priority = typeof PriorityLow | typeof PriorityMedium | typeof PriorityHigh ;
472+ ```
473+
474+ ** Mixed Const Blocks:**
475+ When a const block contains both enum-like constants and other constants, tygo generates an enum for the matching constants and individual const declarations for the rest:
476+
477+ ``` go
478+ // Go input
479+ type UserRole = string
480+ const (
481+ UserRoleAdmin UserRole = " admin"
482+ UserRoleGuest UserRole = " guest"
483+ MaxRetries = 5
484+ DefaultTimeout = 30
485+ )
486+ ```
487+
488+ ``` typescript
489+ // TypeScript output (with enum_style: "enum")
490+ export enum UserRole {
491+ Admin = " admin" ,
492+ Guest = " guest" ,
493+ }
494+ export const MaxRetries = 5 ;
495+ export const DefaultTimeout = 30 ;
496+ ```
497+
498+ ``` typescript
499+ // TypeScript output (with enum_style: "union")
500+ export const UserRoleAdmin = " admin" ;
501+ export const UserRoleGuest = " guest" ;
502+ export type UserRole = typeof UserRoleAdmin | typeof UserRoleGuest ;
503+ export const MaxRetries = 5 ;
504+ export const DefaultTimeout = 30 ;
505+ ```
506+
399507## YAML support
400508
401509Tygo supports generating typings for YAML-serializable objects that can be understood by Go apps.
0 commit comments