Targets · TypeScript · Native modules
Native modules
The hand-written, per-platform code behind NATIVE functions.
Contents
How a Program becomes TypeScript, in parts. Start with the introduction, then follow each part into the rendered output and the runtime it links against.
- Introduction — what the TypeScript target is, and how to read the rest of this section.
- Capabilities — which high-level features are possible, built in or through a library.
- Example — a full program using every keyword, and the TypeScript it renders to.
- Naming considerations — how Program's dotted names are carried into TypeScript.
- Entry point — starting a program and attaching it to a root element.
- Drivers — the native drivers that provide the interface, animation and network.
- Runtime — the Reactive and Context machinery the rendered code links against.
- Native modules — the hand-written JavaScript behind
NATIVEfunctions. - Keywords — how each of Program's keywords renders to TypeScript.
- Project setup — the files and commands to build and run the rendered program.
- Reserved words — the words TypeScript keeps for itself, and how a colliding name is escaped.
Hand-written bodies
Most of a program is written in Program and rendered to TypeScript. A
NATIVE function is the exception: its body is hand-written.
Because JavaScript is valid TypeScript, a native with no ts NATIVE row
reuses its js body verbatim, wrapped in a typed signature; a ts row is
added only when the body needs types the plain JavaScript cannot express.
export function ToString(input: Code.Language.Json.Value): string {
return JSON.stringify(Code.Language.Json.Value._unionToRaw(input)); // js body, typed
}
The registry
Each native module's top-level functions and classes are gathered into a registry so generated
code can reach them by name, with their .d.ts declarations honoured by the compiler.
Drivers, and the runtime, are native too
The drivers are native code —
Ui.Dom.Driver, Animate.Frame.Driver, Network.Web.Driver —
the one place the real platform (document, fetch,
requestAnimationFrame) is touched. Reactive and Context are themselves native modules,
bundled first, so the result is one typed artifact — runtime, native bodies, and program —
that a page or Node can load.