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
- Getting Started - enable
@tsonic/jsin a Tsonic project - Importing APIs - importing from
@tsonic/js/index.js
APIs
- console
- JSON
- Math
- Timers
- Globals (global functions like
parseInt) - JSArray
- Map/Set/WeakMap/WeakSet
- ArrayBuffer + Typed Arrays
- Date
- RegExp
- Number
- 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/jsprovides JavaScript runtime APIs (JSON, Math, Date, timers, JS collections).@tsonic/nodejsprovides Node-style APIs (fs/path/http/crypto/process, etc.) implemented on .NET.
You can enable either or both in a project.