Skip to content

TypeScript types

The SDK and the engine ship their own types, so editors complete and check every call, in TypeScript or in plain JavaScript with // @ts-check. The types stand alone: the protocol’s and the avatars’ types the API uses (Body, Point, PlayerFrame, Gesture, Shape, AgeRange, Locale, Backdrop, BackdropView, GameManifest, …) are inlined, and there is nothing else to install. Every public function and field has its documentation in the types, so hovering a call shows it. The same documentation is on this site as the API reference.

Terminal window
npm install @fabiplay/sdk @fabiplay/engine # the engine only if you use it

The packages are published under the @fabiplay scope from 0.9.0; earlier versions were @fabi-play/*.

import { connect, type PlayerFrame, type Point, type GameManifest } from '@fabiplay/sdk'
import { createEngine, type Engine, type Hand } from '@fabiplay/engine'

Each package has an ES module (dist/index.js) and its declarations (dist/index.d.ts), found through exports. The engine takes the SDK as a peer dependency: install both. createEngine({ play }) takes the Play of connect() or the script tag’s FabiPlay: its play option is typed PlayLike, the few members the engine uses, so both fit and the engine’s types don’t depend on the SDK’s.

A tsconfig.json that works with any bundler (Vite, esbuild, webpack):

{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"noEmit": true
},
"include": ["src"]
}

"moduleResolution": "Bundler" (or "NodeNext") is needed for exports; the old "Node" (node10) does not read it and finds the types through types only for the main entry.

Copy the declaration files next to the scripts, from the packages’ dist folders:

Script Its types Declares
fabi-play.js fabi-play.d.ts FabiPlay, window.FabiPlay, and the types as FabiPlay.PlayerFrame, FabiPlay.Point, …
fabi-play-engine.js fabi-play-engine.d.ts FabiEngine, window.FabiEngine, and FabiEngine.Engine, FabiEngine.Sprite, …
fabi-play-engine.lite.js fabi-play-engine.lite.d.ts The same as fabi-play-engine.d.ts

Then point the editor at them, either at the top of each script:

// @ts-check
/// <reference path="fabi-play.d.ts" />
/// <reference path="fabi-play-engine.lite.d.ts" />
/** @type {FabiPlay.PlayerFrame[]} */
let players = []
FabiPlay.on('players', (e) => (players = e.players))
FabiEngine.createEngine({ play: FabiPlay }).then((engine) => engine.start())

or once for the whole project, in a jsconfig.json (or tsconfig.json) next to them:

{
"compilerOptions": { "checkJs": true, "strict": true, "target": "ES2022", "lib": ["ES2022", "DOM"] },
"include": ["*.js", "fabi-play.d.ts", "fabi-play-engine.lite.d.ts"]
}

The globals are seen by scripts (files without import or export). A TypeScript module that uses the script tag’s FabiPlay says so itself:

import type { FabiPlayGlobal } from '@fabiplay/sdk'
declare const FabiPlay: FabiPlayGlobal