Skip to content

Standard library

Nothing here needs importing. The functions that are in scope everywhere — stringOf, parseInt, Math, Random, Trig, tileOf — have their own page, Globals; the collections — List, Map, Container — have theirs.

src/menu.ts2
text={chosen.length === 0 ? 'No option chosen yet' : `Chose ${chosen}`}
s.lengthNumber of characters
s.toLowerCase()Lowercased copy
s.indexOf(search, from?)Position of search, or -1
s.slice(start, end)The characters between two positions
s.substring(start, end)The same
s.charAt(i)The character at a position, as a string
s.includes(search)Whether search appears
s.startsWith(prefix)Whether it begins with prefix
s.endsWith(suffix)Whether it ends with suffix
s.padStart(length, '0')Left-padded with zeroes. The fill must be '0'
s.concat(other)Joined

Not available: toUpperCase, trim, split, repeat, charCodeAt, match, replace, search, and regular expressions in any form.

=== between two strings is refused. Test a string by its length, by includes, startsWith or endsWith, or use it in a template and let the reader compare:

refused/syntax.ts2
const sameName = (a: String, b: String): Boolean => a === b; // refused

A string that may be missing — the answer of a collection read by a computed key — is discharged with ?? fallback.

+ joins two strings, and a template literal is usually clearer:

src/skills.ts2
<text x={2} y={2} w={{ fill: 4 }} h={{ fill: 4 }} text={`Total level: ${total}`}

Values of any type interpolate — a number is converted for you. + between a string and a number is a type error; put the number in a template, or convert it with stringOf.

+, -, *, / and % are all whole-number. Division truncates: 7 / 2 is 3, and there is nothing to round.

To round a division up, add the divisor less one first: (count + 7) / 8 is count / 8 rounded up. Multiply before you divide when a fraction is involved:

src/reference-language-globals.ts2
<rect x={ROSE / 2 - DOT / 2 + Trig.sinDeg(angle) * RADIUS / TURN}

Whole-number division is what places a grid:

src/inventory.ts2
<layer x={(slot % COLUMNS) * SLOT_W} y={(slot / COLUMNS) * SLOT_H} w={SLOT_W} h={SLOT_H}>

& and | combine whole numbers bit by bit; +=, -=, *=, /=, %=, ++ and -- work on a local. Not available: the shifts << and >>, ^, ~, **, and &&= / ||=.

Math.min, max, clamp, pow, sqrt, Random.int and Trig are on Globals.

An array literal is a constant, for a fixed number of components:

src/shop.ts2
const TABS = ['Buy', 'Sell', 'Info'];
src/shop.ts2
{TABS.map((label, i) => (
<layer x={i * 79} y={0} w={74} h={22} onClick={() => { setTab(i); }}>
arr.map((entry, i) => …)One component per entry

A plain array literal at module level is a constant array. Entries may be records, and each field keeps its type:

src/shop.ts2
const STOCK = [
{ item: Items.BRONZE_DAGGER, name: 'Bronze dagger', price: 12, stock: 30 },
{ item: Items.IRON_SCIMITAR, name: 'Iron scimitar', price: 154, stock: 8 },

There is no arr[i] and no arr.length (TS2E003). Give an entry a name; for a count, use a collection’s .length.

refused/standard-library.ts2
<text x={10} y={42} w={{ fill: 20 }} h={16} font={Fonts.PLAIN} color={Colors.WHITE}
text={`${TABS.length} tabs`} />

There is no push, no filter, no sort, and no way to build an array while the game runs. For anything read while the game runs, declare a collection. For per-row bookkeeping inside a function the server calls, intArray is on the same page.

An enum is the other constant, and the right spelling for a set of names:

src/wares.ts2
enum Category { TOOLS, CARE, SEEDS }

listOf, mapOf, List, Map and Container have their own page: Collections.

None of these names exist: Promise, async/await, console, setTimeout, fetch, JSON, Set, Date, Error, RegExp, Symbol, String(n), Number(s).

Map is ts2’s own keyed collection.

For waiting, see Timers. For text the player types, see Keyboard and input.