Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A Golang struct to TypeScript class/interface converter

License

NotificationsYou must be signed in to change notification settings

tkrajina/typescriptify-golang-structs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Use the commantline tool:

tscriptify -package=package/with/your/models -target=target_ts_file.ts Model1 Model2

If all your structs are in one file, you can convert them with:

tscriptify -package=package/with/your/models -target=target_ts_file.ts path/to/file/with/structs.go

Or by using it from your code:

converter := typescriptify.New()converter.Add(Person{})converter.Add(Dummy{})err := converter.ConvertToFile("ts/models.ts")if err != nil {    panic(err.Error())}

Command line options:

$ tscriptify --helpUsage of tscriptify:-backup string        Directory where backup files are saved-package string        Path of the package with models-target string        Target typescript file

Models and conversion

If thePerson structs contain a reference to theAddress struct, then you don't have to addAddress explicitly. Only fields with a validjson tag will be converted to TypeScript models.

Example input structs:

type Address struct {    Duration float64 `json:"duration"`    Text1    string  `json:"text,omitempty"`    // Ignored:    Text2 string `json:",omitempty"`    Text3 string `json:"-"`}type Dummy struct {    Something string `json:"something"`}type Person struct {    Name      string    `json:"name"`    Nicknames []string  `json:"nicknames"`    Addresses []Address `json:"addresses"`    Dummy     Dummy     `json:"a"`}

Generated TypeScript:

class Dummy {        something : string;}class Address {        duration : number;        text : string;}class Person {        name : string;        nicknames : string[];        addresses : Address[];        a : Dummy;}

In TypeScript you can just cast your javascript object in any of those models:

var person = <Person> {"name":"Me myself","nicknames":["aaa", "bbb"]};console.log(person.name);// The TypeScript compiler will throw an error for this lineconsole.log(person.something);

Custom Typescript code

Any custom code can be added to Typescript models:

class Address {        street : string;        no : number;        //[Address:]        country: string;        getStreetAndNumber() {            return street + " " + number;        }        //[end]}

The lines between//[Address:] and//[end] will be left intact afterConvertToFile().

If your custom code contain methods, then just casting yout object to the target class (with<Person> {...}) won't work because the casted object won't contain your methods.In that case, you can configure the converter to create staticcreateFrom methods:

converter := typescriptify.New()converter.CreateFromMethod = trueconverter.Indent = "    "

The TypeScript code will now be:

class Person {    name: string;    personal_info: PersonalInfo;    nicknames: string[];    addresses: Address[];    static createFrom(source: any) {        var result = new Person();        result.name = source["name"];        result.personal_info = source["personal_info"] ? PersonalInfo.createFrom(source["personal_info"]) : null;        result.nicknames = source["nicknames"];        result.addresses = source["addresses"] ? source["addresses"].map(function(element) { return Address.createFrom(element); }) : null;        return result;    }    //[Person:]    yourMethod = () => {        return "name:" + this.name;    }    //[end]}

And now, instead of casting toPerson you need to:

var person = Person.createFrom({"name":"Me myself","nicknames":["aaa", "bbb"]});

If you use golang JSON structs as responses from your API, you may want to have a common prefix for all the generated models:

converter := typescriptify.New()converter.Prefix("API_")converter.Add(Person{})

The model name will beAPI_Person instead ofPerson.

Custom types

If your field has a type not supported by typescriptify which can be JSONized as is, then you can use thets_type tag to specify the typescript type to use:

type Data struct {    Counters map[string]int `json:"counters" ts_type:"{[key: string]: number}"`}

...results with...

export class Data {        counters: {[key: string]: number};}

If the JSON field needs some special handling before converting it to a javascript object, usets_transform.For example, Dates can be handles this way:

type Data struct {Time time.Time `json:"time" ts_type:"Date" ts_transform:"new Date(__VALUE__)"`}

Generated typescript:

export class Data {    time: Date;    static createFrom(source: any) {        var result = new TestCustomType();        result.time = new Date(source["time"]);        return result;    }}

In this case, you should always useData.createFrom(json) instead of just casting<Data>json.

License

This library is licensed under theApache License, Version 2.0

About

A Golang struct to TypeScript class/interface converter

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors12


[8]ページ先頭

©2009-2025 Movatter.jp