Debian Codegraph @

README.md

raw symbols SCIP
1 (EXP) tygoja
2 [![GoDoc](https://godoc.org/github.com/pocketbase/tygoja?status.svg)](https://pkg.go.dev/github.com/pocketbase/tygoja)
3 ======================================================================
4
5 **tygoja** is a small helper library for generating TypeScript declarations from Go code.
6
7 The generated typings are intended to be used as import helpers to provide [ambient TypeScript declarations](https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html) (aka. `.d.ts`) for [goja](https://github.com/dop251/goja) bindings.
8
9 > **⚠️ Don't use it directly in production! It is not tagged and may change without notice.**
10 >
11 > **It was created to semi-automate the documentation of the goja integration for PocketBase.**
12 >
13 > **Use it only as a reference or as a non-critical step in your dev pipeline.**
14
15 **tygoja** is a heavily modified fork of [tygo](https://github.com/gzuidhof/tygo) and extends its scope with:
16
17 - custom field and method names formatters
18 - types for interfaces (exported and unexported)
19 - types for exported interface methods
20 - types for exported struct methods
21 - types for exported package level functions (_must enable `PackageConfig.WithPackageFunctions`_)
22 - inheritance declarations for embeded structs (_embedded pointers are treated as values_)
23 - autoloading all unmapped argument and return types (_when possible_)
24 - applying the same [goja's rules](https://pkg.go.dev/github.com/dop251/goja#hdr-Nil) when resolving the return types of exported function and methods
25 - combining multiple packages typings in a single output
26 - generating all declarations in namespaces with the packages name (both unmapped and mapped)
27 - preserving comment block new lines
28 - converting Go comment code blocks to Markdown code blocks
29 - and others...
30
31 Note that by default the generated typings are not generated with `export` since the intended usage is to map them to your custom goja bindings.
32 This mapping could be defined in the `Config.Heading` field usually with the `declare` keyword (eg. `declare let someGojaProp: app.Cache`).
33
34 ## Example
35
36 ```go
37 package main
38
39 import (
40 "log"
41 "os"
42
43 "github.com/pocketbase/tygoja"
44 )
45
46 func main() {
47 gen := tygoja.New(tygoja.Config{
48 Packages: map[string][]string{
49 "github.com/pocketbase/tygoja/test/a": {"*"},
50 "github.com/pocketbase/tygoja/test/b": {"*"},
51 "github.com/pocketbase/tygoja/test/c": {"Example2", "Handler"},
52 },
53 Heading: `declare var $app: c.Handler; // bind other fields `,
54 WithPackageFunctions: true,
55 })
56
57 result, err := gen.Generate()
58 if err != nil {
59 log.Fatal(err)
60 }
61
62 if err := os.WriteFile("./types.d.ts", []byte(result), 0644); err != nil {
63 log.Fatal(err)
64 }
65 }
66 ```
67
68 You can also combine it with [typedoc](https://typedoc.org/) to create HTML/JSON docs from the generated declaration(s).
69
70 See the package `/test` directory for example output.
71
72 For a more detailed example you can also explore the [PocketBase's jsvm plugin](https://github.com/pocketbase/pocketbase/tree/develop/plugins/jsvm/internal/docs).
73
74
75 ## Known issues and limitations
76
77 - Multiple versions of the same package may have unexpected declaration since all versions will be under the same namespace.
78 - For easier generation, it relies on TypeScript declarations merging, meaning that the generated types may not be very compact.
79 - Package level functions and constants, that are reserved JS identifier, are prefixed with underscore (eg. `_in()`).