{"id":21121423,"url":"https://github.com/oneofone/struct2ts","last_synced_at":"2025-07-08T21:32:44.525Z","repository":{"id":48083791,"uuid":"133112795","full_name":"OneOfOne/struct2ts","owner":"OneOfOne","description":"Generate Typescript classes/interfaces out of Go structs","archived":false,"fork":false,"pushed_at":"2021-08-07T21:44:38.000Z","size":77,"stargazers_count":143,"open_issues_count":7,"forks_count":15,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-04T11:24:45.178Z","etag":null,"topics":["class","es6","generator","golang","struct","typescript"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OneOfOne.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-12T04:07:48.000Z","updated_at":"2025-02-22T16:44:58.000Z","dependencies_parsed_at":"2022-08-12T18:11:06.450Z","dependency_job_id":null,"html_url":"https://github.com/OneOfOne/struct2ts","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/OneOfOne/struct2ts","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneOfOne%2Fstruct2ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneOfOne%2Fstruct2ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneOfOne%2Fstruct2ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneOfOne%2Fstruct2ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OneOfOne","download_url":"https://codeload.github.com/OneOfOne/struct2ts/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneOfOne%2Fstruct2ts/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264352884,"owners_count":23594987,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["class","es6","generator","golang","struct","typescript"],"created_at":"2024-11-20T03:50:16.498Z","updated_at":"2025-07-08T21:32:42.602Z","avatar_url":"https://github.com/OneOfOne.png","language":"Go","readme":"# struct2ts [![GoDoc](https://godoc.org/github.com/OneOfOne/struct2ts?status.svg)](https://godoc.org/github.com/OneOfOne/struct2ts)\n\nAn extremely simple and powerful Go struct to Typescript Class generator.\n\nInspired by [tkrajina/typescriptify-golang-structs](https://github.com/tkrajina/typescriptify-golang-structs).\n\n## Install\n\n\tgo get -u -v github.com/OneOfOne/struct2ts/...\n\n## Features\n\n* Fairly decent command line interface if you don't wanna write a generator yourself.\n* Automatically handles Go `int64` timestamps `\u003c-\u003e` Javascript `Date`.\n* Automatically handles json tags.\n\n## Options\n\n### There's an extra struct tag to control the output, `ts`, valid options are\n\n* `-` omit this field.\n* `date` handle converting `time.Time{}.Unix() \u003c-\u003e javascript Date`.\n* `,no-null` only valid for struct fields, forces creating a new class rather than using `null` in TS.\n* `,null` allows any field type to be `null`.\n\n## Example\n\n* Input:\n\n```go\ntype OtherStruct struct {\n\tT time.Time `json:\"t,omitempty\"`\n}\n\ntype ComplexStruct struct {\n\tS           string       `json:\"s,omitempty\"`\n\tI           int          `json:\"i,omitempty\"`\n\tF           float64      `json:\"f,omitempty\"`\n\tTS          *int64       `json:\"ts,omitempty\" ts:\"date,null\"`\n\tT           time.Time    `json:\"t,omitempty\"` // automatically handled\n\tNullOther   *OtherStruct `json:\"o,omitempty\"`\n\tNoNullOther *OtherStruct `json:\"nno,omitempty\" ts:\",no-null\"`\n}\n```\n\n* Output:\n\n```ts\n// ... helpers...\n// struct2ts:github.com/OneOfOne/struct2ts_test.ComplexStructOtherStruct\nclass ComplexStructOtherStruct {\n\tt: Date;\n\n\tconstructor(data?: any) {\n\t\tconst d: any = (data \u0026\u0026 typeof data === 'object') ? ToObject(data) : {};\n\t\tthis.t = ('t' in d) ? ParseDate(d.t) : new Date();\n\t}\n\n\ttoObject(): any {\n\t\tconst cfg: any = {};\n\t\tcfg.t = 'string';\n\t\treturn ToObject(this, cfg);\n\t}\n}\n\n// struct2ts:github.com/OneOfOne/struct2ts_test.ComplexStruct\nclass ComplexStruct {\n\ts: string;\n\ti: number;\n\tf: number;\n\tts: Date | null;\n\tt: Date;\n\to: ComplexStructOtherStruct | null;\n\tnno: ComplexStructOtherStruct;\n\n\tconstructor(data?: any) {\n\t\tconst d: any = (data \u0026\u0026 typeof data === 'object') ? ToObject(data) : {};\n\t\tthis.s = ('s' in d) ? d.s as string : '';\n\t\tthis.i = ('i' in d) ? d.i as number : 0;\n\t\tthis.f = ('f' in d) ? d.f as number : 0;\n\t\tthis.ts = ('ts' in d) ? ParseDate(d.ts) : null;\n\t\tthis.t = ('t' in d) ? ParseDate(d.t) : new Date();\n\t\tthis.o = ('o' in d) ? new ComplexStructOtherStruct(d.o) : null;\n\t\tthis.nno = new ComplexStructOtherStruct(d.nno);\n\t}\n\n\ttoObject(): any {\n\t\tconst cfg: any = {};\n\t\tcfg.i = 'number';\n\t\tcfg.f = 'number';\n\t\tcfg.t = 'string';\n\t\treturn ToObject(this, cfg);\n\t}\n}\n// ...exports...\n\n```\n\n## Command Line Usage\n\n```\n➤ struct2ts -h\nusage: struct2ts [\u003cflags\u003e] [\u003cpkg.struct\u003e...]\n\nFlags:\n\t-h, --help                  Show context-sensitive help (also try --help-long\n\t\t\t\t\t\t\t\tand --help-man).\n\t\t--indent=\"\\t\"           Output indentation.\n\t-m, --mark-optional-fields  Add `?` to fields with omitempty.\n\t-6, --es6                   generate es6 code\n\t-C, --no-ctor               Don't generate a ctor.\n\t-T, --no-toObject           Don't generate a Class.toObject() method.\n\t-E, --no-exports            Don't automatically export the generated types.\n\t-D, --no-date               Don't automatically handle time.Unix () \u003c-\u003e JS\n\t\t\t\t\t\t\t\tDate().\n\t-H, --no-helpers            Don't output the helpers.\n\t-N, --no-default-values     Don't assign default/zero values in the ctor.\n\t-i, --interface             Only generate an interface (disables all the other\n\t\t\t\t\t\t\t\toptions).\n\t-s, --src-only              Only output the Go code (helpful if you want to\n\t\t\t\t\t\t\t\tedit it yourself).\n\t-p, --package-name=\"main\"   the package name to use if --src-only is set.\n\t-k, --keep-temp             Keep the generated Go file, ignored if --src-only\n\t\t\t\t\t\t\t\tis set.\n\t-o, --out=\"-\"               Write the output to a file instead of stdout.\n\t-V, --version               Show application version.\n\nArgs:\n\t[\u003cpkg.struct\u003e]  List of structs to convert (github.com/you/auth/users.User,\n\t\t\t\t\tusers.User or users.User:AliasUser).\n\n```\n\n## Advanced\n\n### Custom output per model\n\n```golang\ntype CustomTypescript interface {\n\tRenderCustomTypescript(w io.Writer) (err error)\n}\n```\nIf your model implements a ```RenderCustomTypescript(w io.Writer) (err error)``` function it will inject what ever you \nwrite to the writer at the end of the model. struct2ts will handle the first level of indenting for you.\n\n## TODO\n\n* Use [xast](https://github.com/OneOfOne/xast) to skip reflection.\n* Support annoymous structs.\n* ~~Support ES6.~~\n\n## License\n\nThis project is released under the [BSD 3-clause \"New\" or \"Revised\" License](https://github.com/golang/go/blob/master/LICENSE).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foneofone%2Fstruct2ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foneofone%2Fstruct2ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foneofone%2Fstruct2ts/lists"}