Create a Go enum type and automatically generate the TypeScript equivalent. Produces union types and option arrays for forms.
Create Go enum and TypeScript equivalent for $ARGUMENTS.
Create the enum in app/http/requests/<enum_name>.go:
package requests
type EnumName string
const (
EnumNameValue1 EnumName = "VALUE_1"
EnumNameValue2 EnumName = "VALUE_2"
EnumNameValue3 EnumName = "VALUE_3"
)
GenderType, BookStatus, ConfigTypes)// Simple enum
type GenderType string
const (
GenderTypeMale GenderType = "MALE"
GenderTypeFemale GenderType = "FEMALE"
GenderTypeOther GenderType = "OTHER"
)
// Status enum
type BookStatus string
const (
BookStatusAvailable BookStatus = "AVAILABLE"
BookStatusBorrowed BookStatus = "BORROWED"
BookStatusMaintenance BookStatus = "MAINTENANCE"
BookStatusReserved BookStatus = "RESERVED"
)
// Category enum (Title Case values)
type ConfigTypes string
const (
ConfigTypeFinancing ConfigTypes = "Financing"
ConfigTypeImprovementAspects ConfigTypes = "Improvement Aspects"
ConfigTypeBusinessCategories ConfigTypes = "Business Categories"
)
After creating the Go enum, run the TypeScript generator:
go run . artisan make:ts-enums --source=app/http/requests --output=resources/js/types
This scans app/http/requests/ for Go enum patterns and generates TypeScript files in resources/js/types/.
For each Go enum, it produces a file like resources/js/types/gender_type.ts:
// Auto-generated from Go enum: GenderType
export type GenderType = 'MALE' | 'FEMALE' | 'OTHER';
export const GENDER_TYPE_OPTIONS: Array<{label: string; value: GenderType}> = [
{ label: 'Male', value: 'MALE' },
{ label: 'Female', value: 'FEMALE' },
{ label: 'Other', value: 'OTHER' },
];
import { GenderType, GENDER_TYPE_OPTIONS } from '@/types/gender_type';
<Select
value={formData.gender}
onValueChange={(value) => setFormData({ ...formData, gender: value as GenderType })}
>
<SelectTrigger>
<SelectValue placeholder="Select gender" />
</SelectTrigger>
<SelectContent>
{GENDER_TYPE_OPTIONS.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
Reference the enum in your request validation:
func (r *EntityCreateRequest) Rules(ctx http.Context) map[string]string {
return map[string]string{
"gender": "required|in:MALE,FEMALE,OTHER",
"status": "required|in:AVAILABLE,BORROWED,MAINTENANCE,RESERVED",
}
}
For broader Go-to-TypeScript type conversion (not just enums), consider the guts library from Coder:
go get github.com/coder/guts
guts converts full Go type definitions (structs, interfaces, enums) to TypeScript. It's a Go library (not CLI) - you import it into a generator program. Useful when you need to sync entire model definitions, not just enums.
app/http/requests/resources/js/types/