Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lil5/typex2
Reads a go package's types and export them to typescript interfaces and basic types.
https://github.com/lil5/typex2
cli export generate generator go golang struct type typescript
Last synced: about 1 month ago
JSON representation
Reads a go package's types and export them to typescript interfaces and basic types.
- Host: GitHub
- URL: https://github.com/lil5/typex2
- Owner: lil5
- License: mit
- Created: 2020-10-26T07:18:32.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-11-07T12:07:59.000Z (2 months ago)
- Last Synced: 2024-11-07T12:35:25.936Z (2 months ago)
- Topics: cli, export, generate, generator, go, golang, struct, type, typescript
- Language: Go
- Homepage:
- Size: 41 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Go](https://github.com/lil5/typex2/actions/workflows/go.yml/badge.svg)](https://github.com/lil5/typex2/actions/workflows/go.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/lil5/typex2)](https://goreportcard.com/report/github.com/lil5/typex2)
[![Coverage Status](https://coveralls.io/repos/github/lil5/typex2/badge.svg?branch=master)](https://coveralls.io/github/lil5/typex2?branch=master)# TypeX 2
Reads a go package's types and export them to TypeScript interfaces and basic types.
🆕 Now with support for Dart, Kotlin and Swift!
Great for keeping frontend types the same as your go backend.
This is the follow up of https://github.com/dtgorski/typex
## Installation
```
go install github.com/lil5/[email protected]
```## Usage
```
$ typex2 -l typescript -i ./examples -o ./examples/typex2.ts
```This will do the following;
1. Read all go files inside the path specified (must use one [package name](https://blog.golang.org/package-names)).
2. Generate typescript [types](https://www.typescriptlang.org/docs/handbook/basic-types.html) and [interfaces](https://www.typescriptlang.org/docs/handbook/interfaces.html) from said go files.
3. Write generated content into `./examples/typex2.ts`.## Help
```
$ typex2 -h
NAME:
typex2 - Convert go structs to other language typesUSAGE:
typex2 [global options] command [command options] pathDESCRIPTION:
Useful for generating types from golang json RestAPI projects for a frontend to ingest.Example:
typex2 -l kotlin -i . -o ./classes.kotlinCOMMANDS:
help, h Shows a list of commands or help for one commandGLOBAL OPTIONS:
--lang value, -l value, --language value Language to generate to [typescript, dart, kotlin, swift] (default: typescript)
--input value, -i value Input directory to read go types from (default: ".")
--output value, -o value Output path & file to write translated types to
--help, -h show help
```## Language type mapping
> copy from Typex readme
>
> https://github.com/dtgorski/typex#typescript-type-mappingTypeScript (resp. JavaScript aka ECMAScript) lacks a native integer number type.
The numeric type provided there is inherently a 64 bit float.
You should keep this in mind when working with exported numeric types - this includes `byte` and `rune` type aliases as well.|Go native types|TypeScript|Dart 🆕|Kotlin 🆕|Swift 🆕|
| --- | --- | --- | --- | --- |
|`bool`|`boolean`|`bool`|`Boolean`|`Bool`|
|`string`|`string`|`String`|`String`|`String`|
|`map`|`Record`|`Map`|`Map`|`Dictionary`|
|`interface`|`Record`|`Map`|`Map`|`Dictionary`|
|`struct` `(named)`|`T`|`dynamic`|`Any`|`Any`|
|`struct` `(anonymous)`|`{}`|`dynamic`|`Any`|`Any`|
|`array` `(slice)`|`T[]`|`List`|`Array`|`Array`|
|`complex`[`64`|`128`]|`any`|`dynamic`|`Float`|`Float`|
|`chan`, `func`, `interface`|`any`|`dynamic`|`Any`|`Any`|
|`int`[`8`|`16`|`32`|`64`]|`number`|`int`|`Int`|`Int`|
|`uint`[`8`|`16`|`32`|`64`]|`number`|`int`|`Int`|`Int`|
|`byte`(=`uint8`)|`number`|`int`|`Int`|`Int`|
|`rune`(=`int32`)|`number`|`dynamic`|`Any`|`Any`|
|`float`[`32`|`64`]|`number`|`double`|`Double`|`Double`|
|`uintptr`|`any`|`dynamic`|`Any`|`Any`|
|`*`|`T \| null`|`T?`|`T?`|`T?`|## Differences between typex2 and typex
1. Code legibility.
- Typex2 uses go's strengths in functional programming.
- It also improves separation of concerns, the reading of the go structs and types is separated from the generation of the types in said language.
2. Generated code is instantly written to that same path instead of out putting it to the console.
3. Pointers are possibly nil in go, thus implemented in Typex2.
4. Ability to generate for other languages than TypeScript: Dart, Kotlin, Swift (it's only 300 lines to add a new language)