A TypeScript-to-Go transpiler.
go install github.com/nnstd/gun@latestOr build from source:
git clone https://github.com/nnstd/gun.git
cd gun
go build .Convert TypeScript to Go source:
gun transpile file.ts # print Go source to stdout
gun transpile file.ts -o out/ # write to output directory
gun transpile src/ -o out/ # transpile a directoryTranspile and compile to a native binary:
gun build file.ts # produces ./file binary
gun build file.ts -o mybin # custom output pathTranspile, compile, and execute in one step:
gun run file.ts
gun run file.ts -- --flag arg # pass arguments to the program| Flag | Description |
|---|---|
-o, --output |
Output directory (transpile) or binary path (build) |
-p, --pkg |
Go package name (default: main) |
-v, --verbose |
Verbose output |
--ast |
Print the tree-sitter AST instead of transpiling |
Input (hello.ts):
const greet = (name: string): string => {
return `Hello, ${name}!`;
};
console.log(greet("world"));Transpile and run:
gun run hello.ts
# Hello, world!Gun uses an all-JSValue architecture — all transpiled variables, parameters, and return values are *jsvalue.JSValue. TypeScript type annotations are used for parsing but ignored during code generation. Operations unwrap values temporarily (e.g., jsvalue.Add(a, b)) and re-wrap the results.
TypeScript source
↓ tree-sitter
CST
↓ transformer
Go AST (go/ast)
↓ go/format
Go source
Transpiled code depends on Gun's runtime packages, which provide Node.js-compatible APIs:
runtime/builtin— JSValue type system, array/string/number/object methodsruntime/builtin/console—console.log,console.error, etc.runtime/builtin/math—Math.floor,Math.random, etc.runtime/builtin/json—JSON.parse,JSON.stringifyruntime/fs—fs.readFileSync,fs.writeFileSync, etc.runtime/path—path.join,path.basename, etc.runtime/os—os.homedir,os.platform, etc.runtime/process—process.argv,process.env,process.exit
- Variables and constants (
let,const,var) - Functions (declarations, expressions, arrow functions, default parameters, rest parameters)
- Classes (constructors, methods, inheritance, static members)
- Control flow (
if/else,for,for...of,for...in,while,do...while,switch) - Error handling (
try/catch/finally,throw) - Template literals
- Destructuring (object and array patterns)
- Spread operator
- Enums
- Module system (
import/export) - Built-in globals (
console,Math,JSON,Object,Array,Error,process) - Node.js modules (
fs,path,os,url,crypto,assert, and more)
go build ./... # build everything
go test ./compiler/ # run compiler tests
go test ./... # run all testsSee LICENSE for details.