jsrt

package
v0.20.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package jsrt provides runtime support for TypeScript-to-Go transpiled code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallMethod added in v0.5.0

func CallMethod(obj any, method string, args ...any) any

CallMethod calls a method on an object by name at runtime using reflection. Returns the first return value, or nil if the method doesn't exist.

func Entries added in v0.7.0

func Entries(v any) []any

Entries returns [key, value] pairs from a map or struct (like Object.entries()).

func Flat added in v0.5.0

func Flat(v any) []any

Flat flattens a slice one level: [[1,2],[3]] → [1,2,3].

func FromEntries added in v0.7.0

func FromEntries(entries any) map[string]any

FromEntries creates a map from [key, value] pairs (like Object.fromEntries()).

func GetField

func GetField(v any, name string) any

GetField accesses a field on an any-typed value by name, like JavaScript property access. Supports structs (by exported field name), maps (by original or exported key), and pointers. For maps, tries the original name first, then the exported (PascalCase) name. For structs, tries the exported name first, then the original name.

func GetFieldAs

func GetFieldAs[T any](v any, name string) T

GetFieldAs is a typed version of GetField that returns a value of the specified type.

func GoExportedName added in v0.5.0

func GoExportedName(s string) string

GoExportedName converts a camelCase JS name to Go exported PascalCase.

func Index

func Index(v any, key any) any

Index indexes into a slice, array, map, or string at runtime.

func IsArray added in v0.7.0

func IsArray(v any) bool

IsArray returns true if the value is a slice or array (like Array.isArray()).

func Keys added in v0.7.0

func Keys(v any) []string

Keys returns the keys of a map or struct as a string slice (like Object.keys()).

func Len

func Len(v any) int

Len returns the length of a slice, array, map, string, or channel at runtime.

func OmitFields added in v0.5.0

func OmitFields(obj any, keys ...string) map[string]any

OmitFields returns a copy of a map[string]any with the specified keys removed.

func OmitKeys added in v0.7.0

func OmitKeys(v any, keys ...string) any

OmitKeys returns a shallow copy of the value with the specified keys removed. Works with maps (string keys) and structs (copies to map, omitting named fields).

func Ptr

func Ptr[T any](v T) *T

Ptr returns a pointer to the given value. Useful for passing literals to *T parameters.

func SetIndex

func SetIndex(v any, key any, val any)

SetIndex sets a value at a given index in a slice, array, or map at runtime.

func Throw

func Throw(v any)

Throw panics with a JSError, simulating JavaScript's throw statement.

func ThrowError

func ThrowError(msg string)

ThrowError simulates throw new Error(msg).

func ThrowTypeError

func ThrowTypeError(msg string)

ThrowTypeError simulates throw new TypeError(msg).

func ToAnySlice

func ToAnySlice[T any](s []T) []any

ToAnySlice converts a typed slice to []any.

func ToBool

func ToBool(v any) bool

ToBool converts any value to boolean using JavaScript truthiness rules. nil, 0, "", false → false; everything else → true.

func TypeOf

func TypeOf(v any) string

TypeOf emulates JavaScript's typeof operator at runtime.

func Values added in v0.7.0

func Values(v any) []any

Values returns the values of a map or struct as an any slice (like Object.values()).

Types

type JSError

type JSError struct {
	Value   any
	Message string
	Stack   string
}

JSError represents a JavaScript error thrown with throw statement.

func CatchValue

func CatchValue(r any) *JSError

CatchValue converts a recovered panic value into a *JSError.

func WrapError

func WrapError(err error) *JSError

WrapError wraps a Go error into a JSError for throw.

func (*JSError) Error

func (e *JSError) Error() string

type JSObject

type JSObject struct {
	V any // The underlying value
}

JSObject wraps an any value with JavaScript-like dynamic property access and method calls. Used by the transpiler as a fallback when TypeScript types cannot be statically mapped to Go types. When the TypeScript checker provides concrete type information (including narrowed types), the transpiler generates concrete Go types with type assertions instead.

func Obj

func Obj(v any) *JSObject

Obj wraps a value in a JSObject.

func (*JSObject) Bool

func (o *JSObject) Bool() bool

Bool returns the underlying value using JavaScript truthiness rules.

func (*JSObject) Call

func (o *JSObject) Call(args ...any) *JSObject

Call invokes the underlying value as a function with the given arguments.

func (*JSObject) Float64

func (o *JSObject) Float64() float64

Float64 returns the underlying value as a float64.

func (*JSObject) Get

func (o *JSObject) Get(name string) *JSObject

Get accesses a property by name, returning a new JSObject. Supports structs (exported fields), maps (string keys), and *JSObject nesting.

func (*JSObject) Index

func (o *JSObject) Index(key any) *JSObject

Index returns the element at the given index.

func (*JSObject) Int

func (o *JSObject) Int() int

Int returns the underlying value as an int.

func (*JSObject) IsNil

func (o *JSObject) IsNil() bool

IsNil returns true if the underlying value is nil.

func (*JSObject) Len

func (o *JSObject) Len() int

Len returns the length of the underlying slice, array, map, or string.

func (*JSObject) Set

func (o *JSObject) Set(name string, value any)

Set sets a property by name on the underlying map or struct.

func (*JSObject) String

func (o *JSObject) String() string

String returns the underlying value as a string.

func (*JSObject) Unwrap

func (o *JSObject) Unwrap() any

Unwrap returns the raw underlying value.

Directories

Path Synopsis
Package array provides JavaScript Array.prototype method equivalents using Go generics.
Package array provides JavaScript Array.prototype method equivalents using Go generics.
compat
lodash
Package lodash provides Go adapters for commonly used lodash/lodash-es functions.
Package lodash provides Go adapters for commonly used lodash/lodash-es functions.
uuid
Package uuid provides a Go adapter for the npm "uuid" package.
Package uuid provides a Go adapter for the npm "uuid" package.
zod
Package zod provides a Go adapter for the npm "zod" schema validation library.
Package zod provides a Go adapter for the npm "zod" schema validation library.
Package console provides console.log/error/warn for transpiled TypeScript code.
Package console provides console.log/error/warn for transpiled TypeScript code.
Package fetch provides the fetch() API for transpiled TypeScript code.
Package fetch provides the fetch() API for transpiled TypeScript code.
node
crypto
Package crypto provides Node.js crypto module equivalents for transpiled TypeScript code.
Package crypto provides Node.js crypto module equivalents for transpiled TypeScript code.
fs
Package fs provides Node.js fs module equivalents for transpiled TypeScript code.
Package fs provides Node.js fs module equivalents for transpiled TypeScript code.
http
Package http provides Node.js http module equivalents for transpiled TypeScript code.
Package http provides Node.js http module equivalents for transpiled TypeScript code.
path
Package path provides Node.js path module equivalents for transpiled TypeScript code.
Package path provides Node.js path module equivalents for transpiled TypeScript code.
Package promise provides a Promise[T] type for transpiled async/await TypeScript code.
Package promise provides a Promise[T] type for transpiled async/await TypeScript code.
Package web provides Go struct definitions for Web API types used by transpiled TypeScript code.
Package web provides Go struct definitions for Web API types used by transpiled TypeScript code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL