{"id":37173868,"url":"https://github.com/enterprizesoftware/typescriptify-golang-structs","last_synced_at":"2026-01-14T20:19:11.319Z","repository":{"id":57580616,"uuid":"361803560","full_name":"enterprizesoftware/typescriptify-golang-structs","owner":"enterprizesoftware","description":"A Golang struct to TypeScript class/interface converter","archived":false,"fork":true,"pushed_at":"2021-04-26T15:42:49.000Z","size":210,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-06-20T11:49:49.099Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"tkrajina/typescriptify-golang-structs","license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/enterprizesoftware.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-26T15:39:08.000Z","updated_at":"2021-04-26T15:42:52.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/enterprizesoftware/typescriptify-golang-structs","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/enterprizesoftware/typescriptify-golang-structs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enterprizesoftware%2Ftypescriptify-golang-structs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enterprizesoftware%2Ftypescriptify-golang-structs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enterprizesoftware%2Ftypescriptify-golang-structs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enterprizesoftware%2Ftypescriptify-golang-structs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/enterprizesoftware","download_url":"https://codeload.github.com/enterprizesoftware/typescriptify-golang-structs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enterprizesoftware%2Ftypescriptify-golang-structs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28434422,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T18:57:19.464Z","status":"ssl_error","status_checked_at":"2026-01-14T18:52:48.501Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-14T20:19:10.696Z","updated_at":"2026-01-14T20:19:11.312Z","avatar_url":"https://github.com/enterprizesoftware.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A Golang JSON to TypeScript model converter\n\n## Installation\n\nThe command-line tool:\n\n```\ngo get github.com/tkrajina/typescriptify-golang-structs/tscriptify\n```\n\nThe library:\n\n```\ngo get github.com/tkrajina/typescriptify-golang-structs\n```\n\n## Usage\n\nUse the command line tool:\n\n```\ntscriptify -package=package/with/your/models -target=target_ts_file.ts Model1 Model2\n```\n\nIf you need to import a custom type in Typescript, you can pass the import string:\n\n```\ntscriptify -package=package/with/your/models -target=target_ts_file.ts -import=\"import { Decimal } from 'decimal.js'\" Model1 Model2\n```\n\nIf all your structs are in one file, you can convert them with:\n\n```\ntscriptify -package=package/with/your/models -target=target_ts_file.ts path/to/file/with/structs.go\n```\n\nOr by using it from your code:\n\n```golang\nconverter := typescriptify.New().\n    Add(Person{}).\n    Add(Dummy{})\nerr := converter.ConvertToFile(\"ts/models.ts\")\nif err != nil {\n    panic(err.Error())\n}\n```\n\nCommand line options:\n\n```\n$ tscriptify --help\nUsage of tscriptify:\n-backup string\n        Directory where backup files are saved\n-package string\n        Path of the package with models\n-target string\n        Target typescript file\n```\n\n## Models and conversion\n\nIf the `Person` structs contain a reference to the `Address` struct, then you don't have to add `Address` explicitly. Only fields with a valid `json` tag will be converted to TypeScript models.\n\nExample input structs:\n\n```golang\ntype Address struct {\n\tCity    string  `json:\"city\"`\n\tNumber  float64 `json:\"number\"`\n\tCountry string  `json:\"country,omitempty\"`\n}\n\ntype PersonalInfo struct {\n\tHobbies []string `json:\"hobby\"`\n\tPetName string   `json:\"pet_name\"`\n}\n\ntype Person struct {\n\tName         string       `json:\"name\"`\n\tPersonalInfo PersonalInfo `json:\"personal_info\"`\n\tNicknames    []string     `json:\"nicknames\"`\n\tAddresses    []Address    `json:\"addresses\"`\n\tAddress      *Address     `json:\"address\"`\n\tMetadata     []byte       `json:\"metadata\" ts_type:\"{[key:string]:string}\"`\n\tFriends      []*Person    `json:\"friends\"`\n}\n```\n\nGenerated TypeScript:\n\n```typescript\nexport class Address {\n    city: string;\n    number: number;\n    country?: string;\n\n    constructor(source: any = {}) {\n        if ('string' === typeof source) source = JSON.parse(source);\n        this.city = source[\"city\"];\n        this.number = source[\"number\"];\n        this.country = source[\"country\"];\n    }\n}\nexport class PersonalInfo {\n    hobby: string[];\n    pet_name: string;\n\n    constructor(source: any = {}) {\n        if ('string' === typeof source) source = JSON.parse(source);\n        this.hobby = source[\"hobby\"];\n        this.pet_name = source[\"pet_name\"];\n    }\n}\nexport class Person {\n    name: string;\n    personal_info: PersonalInfo;\n    nicknames: string[];\n    addresses: Address[];\n    address?: Address;\n    metadata: {[key:string]:string};\n    friends: Person[];\n\n    constructor(source: any = {}) {\n        if ('string' === typeof source) source = JSON.parse(source);\n        this.name = source[\"name\"];\n        this.personal_info = this.convertValues(source[\"personal_info\"], PersonalInfo);\n        this.nicknames = source[\"nicknames\"];\n        this.addresses = this.convertValues(source[\"addresses\"], Address);\n        this.address = this.convertValues(source[\"address\"], Address);\n        this.metadata = source[\"metadata\"];\n        this.friends = this.convertValues(source[\"friends\"], Person);\n    }\n\n\tconvertValues(a: any, classs: any, asMap: boolean = false): any {\n\t\tif (!a) {\n\t\t\treturn a;\n\t\t}\n\t\tif (a.slice) {\n\t\t\treturn (a as any[]).map(elem =\u003e this.convertValues(elem, classs));\n\t\t} else if (\"object\" === typeof a) {\n\t\t\tif (asMap) {\n\t\t\t\tfor (const key of Object.keys(a)) {\n\t\t\t\t\ta[key] = new classs(a[key]);\n\t\t\t\t}\n\t\t\t\treturn a;\n\t\t\t}\n\t\t\treturn new classs(a);\n\t\t}\n\t\treturn a;\n\t}\n}\n```\n\nIf you prefer interfaces, the output is:\n\n```typescript\nexport interface Address {\n    city: string;\n    number: number;\n    country?: string;\n}\nexport interface PersonalInfo {\n    hobby: string[];\n    pet_name: string;\n}\nexport interface Person {\n    name: string;\n    personal_info: PersonalInfo;\n    nicknames: string[];\n    addresses: Address[];\n    address?: Address;\n    metadata: {[key:string]:string};\n    friends: Person[];\n}\n```\n\nIn TypeScript you can just cast your json object in any of those models:\n\n```typescript\nvar person = \u003cPerson\u003e {\"name\":\"Me myself\",\"nicknames\":[\"aaa\", \"bbb\"]};\nconsole.log(person.name);\n// The TypeScript compiler will throw an error for this line\nconsole.log(person.something);\n```\n\n## Custom Typescript code\n\nAny custom code can be added to Typescript models:\n\n```typescript\nclass Address {\n        street : string;\n        no : number;\n        //[Address:]\n        country: string;\n        getStreetAndNumber() {\n            return street + \" \" + number;\n        }\n        //[end]\n}\n```\n\nThe lines between `//[Address:]` and `//[end]` will be left intact after `ConvertToFile()`.\n\nIf your custom code contain methods, then just casting yout object to the target class (with `\u003cPerson\u003e {...}`) won't work because the casted object won't contain your methods.\n\nIn that case use the constructor:\n\n```typescript\nvar person = new Person({\"name\":\"Me myself\",\"nicknames\":[\"aaa\", \"bbb\"]});\n```\n\nIf you use golang JSON structs as responses from your API, you may want to have a common prefix for all the generated models:\n\n```golang\nconverter := typescriptify.New().\nconverter.Prefix = \"API_\"\nconverter.Add(Person{})\n```\n\nThe model name will be `API_Person` instead of `Person`.\n\n## Custom types\n\nIf your field has a type not supported by typescriptify which can be JSONized as is, then you can use the `ts_type` tag to specify the typescript type to use:\n\n```golang\ntype Data struct {\n    Counters map[string]int `json:\"counters\" ts_type:\"CustomType\"`\n}\n```\n\n...will create:\n\n```typescript\nexport class Data {\n        counters: CustomType;\n}\n```\n\nIf the JSON field needs some special handling before converting it to a javascript object, use `ts_transform`.\nFor example:\n\n```golang\ntype Data struct {\n    Time time.Time `json:\"time\" ts_type:\"Date\" ts_transform:\"new Date(__VALUE__)\"`\n}\n```\n\nGenerated typescript:\n\n```typescript\nexport class Date {\n\ttime: Date;\n\n    constructor(source: any = {}) {\n        if ('string' === typeof source) source = JSON.parse(source);\n        this.time = new Date(source[\"time\"]);\n    }\n}\n```\n\nIn this case, you should always use `new Data(json)` instead of just casting `\u003cData\u003ejson`.\n\nIf you use a custom type that has to be imported, you can do the following:\n\n```golang\nconverter := typescriptify.New()\nconverter.AddImport(\"import Decimal from 'decimal.js'\")\n```\n\nThis will put your import on top of the generated file.\n\n## Global custom types\n\nAdditionally, you can tell the library to automatically use a given Typescript type and custom transformation for a type:\n\n```golang\nconverter := New()\nconverter.ManageType(time.Time{}, TypeOptions{TSType: \"Date\", TSTransform: \"new Date(__VALUE__)\"})\n```\n\nThis is how now `time.Time` is managed in the library by default.\n\nIf you only want to change `ts_transform` but not `ts_type`, you can pass an empty string:\n\n## Enums\n\nThere are two ways to create enums. \n\n### Enums with TSName()\n\nIn this case you must provide a list of enum values and the enum type must have a `TSName() string` method\n\n```golang\ntype Weekday int\n\nconst (\n\tSunday Weekday = iota\n\tMonday\n\tTuesday\n\tWednesday\n\tThursday\n\tFriday\n\tSaturday\n)\n\nvar AllWeekdays = []Weekday{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, }\n\nfunc (w Weekday) TSName() string {\n\tswitch w {\n\tcase Sunday:\n\t\treturn \"SUNDAY\"\n\tcase Monday:\n\t\treturn \"MONDAY\"\n\tcase Tuesday:\n\t\treturn \"TUESDAY\"\n\tcase Wednesday:\n\t\treturn \"WEDNESDAY\"\n\tcase Thursday:\n\t\treturn \"THURSDAY\"\n\tcase Friday:\n\t\treturn \"FRIDAY\"\n\tcase Saturday:\n\t\treturn \"SATURDAY\"\n\tdefault:\n\t\treturn \"???\"\n\t}\n}\n```\n\nIf this is too verbose for you, you can also provide a list of enums and enum names:\n\n```golang\nvar AllWeekdays = []struct {\n\tValue  Weekday\n\tTSName string\n}{\n\t{Sunday, \"SUNDAY\"},\n\t{Monday, \"MONDAY\"},\n\t{Tuesday, \"TUESDAY\"},\n\t{Wednesday, \"WEDNESDAY\"},\n\t{Thursday, \"THURSDAY\"},\n\t{Friday, \"FRIDAY\"},\n\t{Saturday, \"SATURDAY\"},\n}\n```\n\nThen, when converting models `AddEnum()` to specify the enum:\n\n```golang\n    converter := New().\n        AddEnum(AllWeekdays)\n```\n\nThe resulting code will be:\n\n```typescript\nexport enum Weekday {\n\tSUNDAY = 0,\n\tMONDAY = 1,\n\tTUESDAY = 2,\n\tWEDNESDAY = 3,\n\tTHURSDAY = 4,\n\tFRIDAY = 5,\n\tSATURDAY = 6,\n}\nexport class Holliday {\n\tname: string;\n\tweekday: Weekday;\n}\n```\n\n## License\n\nThis library is licensed under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenterprizesoftware%2Ftypescriptify-golang-structs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenterprizesoftware%2Ftypescriptify-golang-structs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenterprizesoftware%2Ftypescriptify-golang-structs/lists"}