Skip to main content

Runtime helpers (luau2ts/runtime)

The native emit mode injects imports from luau2ts/runtime for stdlib semantics that don't map 1:1 to JavaScript. These helpers are exposed via the package's ./runtime subpath export so consumers of the compiled output can resolve them at runtime.

import { lualen, luaIndex, pairKeys } from 'luau2ts/runtime';

What's there

HelperReplacesNotes
lualen(v)#vLength operator that respects Luau's array semantics.
luaIndex(t, k)t[k] for unknown-typed k1-indexed when k is numeric, raw when it's a string.
pairKeys(t)pairs(t) keysYields raw keys, with Instance-keyed table support via the global key reifier.
pairValue(t, k)t[k] after pairsReifier-aware value lookup.
genericIter(it)Generic-for iterator tripleLifts a callable / table into the standard iter/state/control triple.
multiret(v)Multi-return destructuringWraps a single-value return into a tuple so [a, b] = f() works.
truthy(v)Luau boolean truthinessTreats 0 and '' as truthy (unlike JS).
concat(a, b).. operatorString concat with Luau's coercion rules.
tostring(v), tonumber(v)Lua stdlibRound-trippable conversions.
pcall, xpcall, errorLua error handlingReturns [ok, ...] tuples.
setmetatable, getmetatableMetatablesStored on a hidden symbol.

The full set is exported from the package's ./runtime entry point. Run node -e "console.log(Object.keys(require('luau2ts/runtime')))" to print the live list.

Tree-shaking

Each emitted file imports only the helpers it actually needs. The helpers field on CompileResult lists them.

Host-runtime hooks

pairKeys / pairValue consult an optional global key reifier:

(globalThis as any).__rbxKeyReifier = (rawKey) => { /* ... */ };

A host Roblox runtime that supports Instance-keyed tables can install this hook so that iteration over a table keyed by Instances returns the original Instance objects (rather than their stringified names).