TrigScript beside TypeScript
Outside program() there is nothing to compare: the script is TypeScript, checked by the
TypeScript compiler and run when it is applied, with the whole language and its standard
library. This table is about the inside of a program, where what is written has to become
something the game can do. Same means it is written and behaves as in TypeScript; what
differs is said, and what is missing is refused with a message as you type, never passed
over in silence.
| TypeScript | In a program | What is different |
|---|---|---|
Types: annotations, interface, type, unions of literals, tuples, generic functions, as, ! |
Same | Erased, as in TypeScript. Some decide how a value is kept: u8, u16 and u32 are widths, and a Map<UnitType, V> is kept differently from a Map<number, V>. A class takes no type parameters. |
let, const, var |
Same | A variable needs a first value (let n = 0). A const the script already knows is worked out when the script is applied and takes nothing from the map. |
number |
Different | A whole number of 32 bits, signed, wrapping at its ends. No fractions, no NaN, no bigint. / is whole division towards zero, and dividing by 0 gives 0. u8 and u16 (which stop at their ends) and u32 are added. |
boolean |
Same | Assigned with = only: no ||= or &&=. |
string |
Mostly | A value with no length to declare. length, s[i] and slice count characters, so they differ from JavaScript only for an emoji or a rare ideograph. A made text holds 1 023 bytes. No split, replace, trim, toUpperCase, parseInt or regular expressions. |
null, undefined |
Units only | Unit | null is real: first(…), if (u), u?.kill(). A number, a boolean or a text is never either, so where JavaScript would give undefined — pop(), find(), get() — say what it is then: xs.pop() ?? 0. No optional fields or parameters (y?: number); a parameter's default (y = 1) is there. |
Operators: + - * / %, comparisons, && || !, c ? a : b, the bitwise ones, ??, ?., ++ --, the compound assignments |
Same | == and === are one thing, since nothing is coerced. Missing: **, typeof, in, delete. |
if, while, do, for, for…of, switch, break, continue |
Same | A loop runs all its rounds within one frame of the game, so one that never ends must sleep() on every path — also a while (true) that is only left by a break. No for…in, no labels. |
| Functions: parameters by value, defaults, rest parameters, recursion, generics | Same | A function that calls itself cannot sleep() and has a depth limit. One declared inside another does not see the outer one's variables, only the program's. It returns a number, a boolean, a text, a unit or an instance — not a record or an array it made: hand it the one to fill in. |
| Arrow functions, closures, functions as values | Callbacks only | An arrow is written where a method takes it — xs.map((x) => x + bonus) — and sees every variable in reach. It cannot be kept in a variable, an array, a field or a return value. |
| Object literals | Records | A variable a field: nested, passed by reference, spread, taken apart by patterns. The shape is fixed: no p[key] with a key that varies, no methods on a literal (a class has them), no Object.keys. |
| Destructuring and spread | Same | ...rest takes the tail of an array of numbers or booleans. |
| Arrays | Mostly | Fixed or growing; of numbers, booleans, texts, units, records, instances and arrays. push, pop, length, fill, includes, indexOf, the methods that take a function, sort, reverse, slice, concat, toSorted, toReversed, join of texts, Array.from. No shift, unshift, splice, at or flat, and map makes numbers or booleans. sort wants its function. An index past the end reads 0 and stores nothing. const b = a is not a second name for an array. |
Classes: fields, constructor, methods, get / set, static, private, readonly, extends, super, abstract, implements, instanceof |
Same | Declared inside the program. The class of every instance is settled when the script is applied: an array of instances holds one class, and a function gives back an instance only when every return gives the same one. No decorators, no class written as a value. |
Map, Set |
Mostly | Keys are numbers, units or ids of the game; values are numbers or booleans. They go through their keys in the order the keys went in, as JavaScript's do — a table keyed by ids of the game goes by id. |
enum |
Outside | Declared above the program, its members are numbers a program can use. |
try / catch / throw |
Missing | The game has no exceptions. What is always a mistake — an index past the end, no room left for an array, a function that called itself too deep — the game says in red, and Simulate lists. |
async / await, promises, generators, timers |
Missing | sleep() is how a program waits. Something on its own clock is another program(). |
import |
Same | Between the script's files, and from "trigscript"; nothing else. A function that another file's program calls is made with game(). |
| The standard library | A little | Math.min, Math.max, Math.abs, String(n), Array.from, new Array(n).fill(v). print() stands in for console.log and random(n) for Math.random(). No JSON, Date or Object.*, and no Math.sqrt on a variable: look the value up in a list the script worked out. |
| — | Added | What TypeScript has no word for: sleep, rose and once, reads of the game (minerals(p)), units as objects and loops over them, stats(), keys, the mouse and chat, shared(), per-player programs. |