Giter Site home page Giter Site logo

okbug / go2ts Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stirlingmarketinggroup/go2ts

0.0 0.0 0.0 6.1 MB

Online Golang Struct to TypeScript Interface Converter

Home Page: https://StirlingMarketingGroup.github.io/go2ts/

License: MIT License

JavaScript 65.16% Go 13.12% HTML 14.74% SCSS 6.98%

go2ts's Introduction

Golang Struct to TypeScript Interface

This tool converts Go structs to TypeScript interfaces. Paste a Go struct on the left and the TypeScript interface will be generated on the right. Custom types will be left alone for you to fix yourself. time.Time are converted to strings, because this makes sense for our use case. The "json" struct tag will change the name of the property. Any pointers or "omitempty" fields will be optional.

This uses Go compiled to web assembly, so sorry IE users. But not really.

Because this is all done with wasm, that means we have no server costs, not even lambda functions! It also means we aren't storing or logging anyone's requests in any way.

Example

An example Go struct...

type user struct {
    ID xid.Xid `json:"id"`
    Name string `json:"name"`
    age  int `json:"age,omitempty"` // unexported

    Map map[int]*string

    Orders []struct {
        InvoiceNumber int `json:"invoiceNumber"`
        Quantity int `json:"qty"`
        Details interface{}
        Created time.Time
    }

    Created time.Time
}

... and the generated TypeScript

declare interface user {
    id: xid.Xid;
    name: string;
    Map: { [key: number]: string | undefined};
    Orders: {
        invoiceNumber: number;
        qty: number;
        Details: any;
        Created: string;
    }[];
    Created: string;
}

Other tools

Those two tools approach this the same way. In fact, one is a fork of the other. Both use Go generation to generate your tagged stucts and convert them to typescript interfaces.

This tool, however, doesn't use Go generation. Instead, we use go/parser to parse the provided go into a syntax tree, and then we loop through the StructType nodes within the tree to generate our typescript. Doing it this way allows us to convert any struct to typescript via the browser, vs having to tag our structs and generate them all each time. I'm sure their libraries are extremely useful in their workflow, but personally, we like to be a little less coupled than that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.