TypeScript
Types ship with the package; configure your project to resolve the right entry.
ball2d includes TypeScript declarations for both entries. You don't need a separate @types package.
Configure your project
For a Node.js host, resolve the package exports the way Node does:
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ES2022",
"lib": ["ES2022"],
"types": ["node"],
"strict": true
}
}The ball2d/node declarations do not depend on DOM or WebRTC types, so lib can stay at ES2022 and you don't need skipLibCheck.
Import types
Public types are exported alongside the functions:
import {
createRoom,
type HostPlayer,
type NodeRoom,
} from 'ball2d/node';
function welcome(room: NodeRoom, player: HostPlayer) {
return room.sendAnnouncement(
`Welcome, ${player.name}`,
player.id,
null,
'bold',
0,
);
}Some types, such as MatchState, are not exported by name. Reach them through the values that return them:
type MatchState = ReturnType<NodeRoom['getState']>;The types reference lists every public type and where to import it from.
Callbacks and this
Room callbacks are plain properties on the room. Each runs with the room as this, but arrow functions that close over room are clearer and keep their types:
room.onPlayerJoin = (player) => {
void room.setPlayerTeam(player.id, 1);
};