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
go install github.com/lil5/typex2@1.2.0
$ typex2 -l typescript -i ./examples -o ./examples/typex2.ts
This will do the following;
- Read all go files inside the path specified (must use one package name).
- Generate typescript types and interfaces from said go files.
- Write generated content into
./examples/typex2.ts
.
$ typex2 -h
NAME:
typex2 - Convert go structs to other language types
USAGE:
typex2 [global options] command [command options] path
DESCRIPTION:
Useful for generating types from golang json RestAPI projects for a frontend to ingest.
Example:
typex2 -l kotlin -i . -o ./classes.kotlin
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL 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
copy from Typex readme
TypeScript (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<K, V> |
Map<K, V> |
Map<K, V> |
Dictionary<K, V> |
interface |
Record<string, any> |
Map<String, dynamic> |
Map<String, Any> |
Dictionary<String, Any> |
struct (named) |
T |
dynamic |
Any |
Any |
struct (anonymous) |
{} |
dynamic |
Any |
Any |
array (slice) |
T[] |
List<T> |
Array<T> |
Array<T> |
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? |
- 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.
- Generated code is instantly written to that same path instead of out putting it to the console.
- Pointers are possibly nil in go, thus implemented in Typex2.
- Ability to generate for other languages than TypeScript: Dart, Kotlin, Swift (it's only 300 lines to add a new language)