Tsonic GitHub
Edit on GitHub

JavaScript Runtime Compatibility (@tsonic/js)

Tsonic targets the .NET BCL by default. If you want JavaScript-style runtime APIs (like JSON, Math, timers, Date, RegExp, JS arrays, etc.), use @tsonic/js.

This is not Node.js. It’s a JS-semantics runtime implemented in .NET so TypeScript code can opt into familiar JavaScript behavior where needed.

Table of Contents

Getting Started

  1. Getting Started - enable @tsonic/js in a Tsonic project
  2. Importing APIs - importing from @tsonic/js/index.js

APIs

  1. console
  2. JSON
  3. Math
  4. Timers
  5. Globals (global functions like parseInt)
  6. JSArray
  7. Map/Set/WeakMap/WeakSet
  8. ArrayBuffer + Typed Arrays
  9. Date
  10. RegExp
  11. Number
  12. String

Overview

In Tsonic projects you import JS runtime APIs from @tsonic/js/index.js:

import { console, JSON } from "@tsonic/js/index.js";

export function main(): void {
  const value = JSON.parse<{ x: number }>('{"x": 1}');
  console.log(JSON.stringify(value));
}

Relationship to @tsonic/nodejs

  • @tsonic/js provides JavaScript runtime APIs (JSON, Math, Date, timers, JS collections).
  • @tsonic/nodejs provides Node-style APIs (fs/path/http/crypto/process, etc.) implemented on .NET.

You can enable either or both in a project.