Skip to content

Repository files navigation

struct2ts GoDoc

An extremely simple and powerful Go struct to Typescript Class generator.

Inspired by tkrajina/typescriptify-golang-structs.

Install

go get -u -v github.com/OneOfOne/struct2ts/...

Features

  • Fairly decent command line interface if you don't wanna write a generator yourself.
  • Automatically handles Go int64 timestamps <-> Javascript Date.
  • Automatically handles json tags.

Options

There's an extra struct tag to control the output, ts, valid options are

  • - omit this field.
  • date handle converting time.Time{}.Unix() <-> javascript Date.
  • ,no-null only valid for struct fields, forces creating a new class rather than using null in TS.
  • ,null allows any field type to be null.

Example

  • Input:
typeOtherStructstruct {
T time.Time`json:"t,omitempty"`
}
typeComplexStructstruct {
Sstring`json:"s,omitempty"`Iint`json:"i,omitempty"`Ffloat64`json:"f,omitempty"`TS*int64`json:"ts,omitempty" ts:"date,null"`T time.Time`json:"t,omitempty"`// automatically handledNullOther*OtherStruct`json:"o,omitempty"`NoNullOther*OtherStruct`json:"nno,omitempty" ts:",no-null"`
}
  • Output:
// ... helpers...// struct2ts:github.com/OneOfOne/struct2ts_test.ComplexStructOtherStructclassComplexStructOtherStruct{t: Date;constructor(data?: any){constd: any=(data&&typeofdata==='object') ? ToObject(data) : {};this.t=('t'ind) ? ParseDate(d.t) : newDate();}toObject(): any{constcfg: any={};cfg.t='string';returnToObject(this,cfg);}}// struct2ts:github.com/OneOfOne/struct2ts_test.ComplexStructclassComplexStruct{s: string;i: number;f: number;ts: Date|null;t: Date;o: ComplexStructOtherStruct|null;nno: ComplexStructOtherStruct;constructor(data?: any){constd: any=(data&&typeofdata==='object') ? ToObject(data) : {};this.s=('s'ind) ? d.sasstring : '';this.i=('i'ind) ? d.iasnumber : 0;this.f=('f'ind) ? d.fasnumber : 0;this.ts=('ts'ind) ? ParseDate(d.ts) : null;this.t=('t'ind) ? ParseDate(d.t) : newDate();this.o=('o'ind) ? newComplexStructOtherStruct(d.o) : null;this.nno=newComplexStructOtherStruct(d.nno);}toObject(): any{constcfg: any={};cfg.i='number';cfg.f='number';cfg.t='string';returnToObject(this,cfg);}}// ...exports...

Command Line Usage

➤ struct2ts -h
usage: struct2ts [<flags>] [<pkg.struct>...]
Flags:
-h, --help Show context-sensitive help (also try --help-long
and --help-man).
--indent="\t" Output indentation.
-m, --mark-optional-fields Add `?` to fields with omitempty.
-6, --es6 generate es6 code
-C, --no-ctor Don't generate a ctor.
-T, --no-toObject Don't generate a Class.toObject() method.
-E, --no-exports Don't automatically export the generated types.
-D, --no-date Don't automatically handle time.Unix () <-> JS
Date().
-H, --no-helpers Don't output the helpers.
-N, --no-default-values Don't assign default/zero values in the ctor.
-i, --interface Only generate an interface (disables all the other
options).
-s, --src-only Only output the Go code (helpful if you want to
edit it yourself).
-p, --package-name="main" the package name to use if --src-only is set.
-k, --keep-temp Keep the generated Go file, ignored if --src-only
is set.
-o, --out="-" Write the output to a file instead of stdout.
-V, --version Show application version.
Args:
[<pkg.struct>] List of structs to convert (github.com/you/auth/users.User,
users.User or users.User:AliasUser).

Advanced

Custom output per model

typeCustomTypescriptinterface {
RenderCustomTypescript(w io.Writer) (errerror)
}

If your model implements a RenderCustomTypescript(w io.Writer) (err error) function it will inject what ever you write to the writer at the end of the model. struct2ts will handle the first level of indenting for you.

TODO

  • Use xast to skip reflection.
  • Support annoymous structs.
  • Support ES6.

License

This project is released under the BSD 3-clause "New" or "Revised" License.

About

Generate Typescript classes/interfaces out of Go structs

Topics

Resources

Stars

142 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages