Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

TypeScript Backend

First target, used to validate the IR and semantics. Status: v2 — verified; tsc --strict is clean and the full conformance suite passes in vitest.

Representation

LTTypeScript
Int (i64)bigint + BigInt.asIntN(64, …) via lt.iadd/isub/imul/idiv/irem/ineg
Floatnumber
Strstring; index/len are counted per code point ([...s])
Boolboolean
List<T>lt.LList<T> (immutable, copy-on-write)
Map<K,V>lt.LMap<K,V> (entries ordered by Ord, lookup via binary search O(log n))
Dict<V>lt.LDict<V> (string keys, insertion order, Map index)
fn(A) -> R(a: A) => R; a lambda becomes an arrow function
Valueprelude enum value, rendered like a regular enum; json.parse/stringify in lt
Set<T>lt.LSet<T>
Option<T>lt.Opt<T> = union {tag:"some"|"none", values, __lt_meta}
Result<T,E>lt.Res<T,E> = union {tag:"ok"|"err", …}
enuma single identifier: export type Enum + export const Enum, variants called as Enum.Variant(...); variant order & display live in the shared Enum_variant
structclass with readonly fields, object constructor { field: … }, __lt_eq, __lt_cmp, __lt_to_string; embed fields remain regular fields and widens to users.tableInfo
package-level functionsexport function name(...)
private functionsexport function _name(...) — still exported so test files can call them, but marked _ as internal
package bindingsexport const name: T = … (private: _name)
packageone module file: src/<package>.ts
receiver functionsremain module functions; p.len() is lowered to len(p)
Displaylt.disp(value); the owning package’s to_string is called by __lt_to_string
Eq / Ordlt.eq(a, b) / lt.cmp(a, b)
? on Result/Optionconst t = …; if (t.tag === "err"/"none") return t;
templateconcatenation + lt.disp(...)
tagged templatecall to a package-level function with LList<string> + LList<T>; values are not disped
matchswitch (value.tag)
JSXnew Struct({ field: …, children: new LList([...]) })
expect.eq etc.lt.expectEqual / expectNotEqual / expectTrue / expectFalse
test "title"it("title", …) in tests/<package>.test.ts

Package structure

out/ts/
  _lingua_rt.ts        inline runtime (embedded from runtimes/ts/runtime.ts)
  package.json
  tsconfig.json        strict, ES2022, noEmit
  src/<package>.ts     one file per LT package
  tests/<package>.test.ts  only from `lt test`

Cross-package imports use the namespace m<id> (import * as m1 from "./user"), so names never collide. The runtime alias is always lt, and a user declaration named lt is mangled to lt_.

Naming

  • fields, functions, parameters, local variables: snake_case → camelCase
  • private functions/bindings: _ prefix (_doubleTwice)
  • structs, enums, variants: keep PascalCase
  • TypeScript keywords get a _ suffix
  • test titles are used verbatim in it("…")

Test

lt test -t ts tests/conformance/*.lt --out out/conformance --run

--run runs vitest. tsconfig.json includes tests/**/*.ts, so tsc --strict also checks the test files.

Limitations

  • Ordering is not exposed on the language surface yet.
  • Builtins are only available as receivers (xs.map(…)), not list.map(xs, …).