Use TypeScript
Set up @falloutmp/types so your editor autocompletes the whole scripting API and the compiler catches a wrong call before the server does.
The declarations in @falloutmp/types are
generated from the bindings the server and the client register, so an editor pointed at them
autocompletes the real API and nothing else. The default gamemode is already set up this way; this
page sets up one of your own.
Install the declarations
Section titled “Install the declarations”npm init -ynpm install --save-dev @falloutmp/types typescriptPin the version to your server’s release. The declarations are published with every release, and a server that is newer than your declarations has calls you cannot see yet; one that is older has calls that will not work.
The server half
Section titled “The server half”A resource runs in the server’s own isolate, not in a plain Node program, so types names only the
scripting declarations. Nothing from @types/node is there.
{ "compilerOptions": { "target": "ES2022", "module": "CommonJS", "moduleResolution": "node", "lib": ["ES2022"],
"rootDir": "src/server", "outDir": "dist/server",
"strict": true, "noUnusedLocals": true, "noImplicitReturns": true, "noUncheckedIndexedAccess": true, "forceConsistentCasingInFileNames": true,
"types": ["@falloutmp/types/server"] }, "include": ["src/server/**/*.ts"]}module: CommonJS matters: a resource’s files are loaded through require(), so an ES module emit
will not run.
Point the manifest at the emit rather than the source:
{ "name": "survival-kit", "version": "0.1.0", "private": true, "scripts": { "build": "tsc -p tsconfig.json", "watch": "tsc -p tsconfig.json --watch" }, "devDependencies": { "@falloutmp/types": "0.1.0", "typescript": "^5.9.2" }, "mafiahub": { "serverScripts": ["dist/server/index.js"], "clientScripts": ["client/index.js"] }}The client half
Section titled “The client half”The client runs a different API, so it gets its own project with its own types. Keeping the two in
one project is the mistake that lets a server call compile inside client code and fail at runtime.
{ "compilerOptions": { "target": "ES2022", "module": "CommonJS", "moduleResolution": "node", "lib": ["ES2022"],
"rootDir": "src/client", "outDir": "dist/client",
"strict": true, "types": ["@falloutmp/types/client"] }, "include": ["src/client/**/*.ts"]}{ "scripts": { "build": "tsc -p tsconfig.json && tsc -p tsconfig.client.json", "watch": "tsc -p tsconfig.json --watch" }}What you get
Section titled “What you get”Globals are typed, so the arguments of a native event are too, and a typo is a compiler error rather
than a quiet undefined at two in the morning:
Events.on("playerDeath", (player: Player, killer: Player | null) => { console.log(killer ? `${killer.nickname} killed ${player.nickname}` : `${player.nickname} died`); setTimeout(() => player.respawn(), 5000);});Refusable events are typed by their return value, so a handler that drops a hit reads as one:
Events.on("playerHit", (attacker: Player, victim: Player, damage: number, kind: string) => { if (kind === "hazard") return; if (attacker.state.get("team") === victim.state.get("team")) return false; return damage > 0;});Values the API cannot know the shape of arrive as unknown, which is the compiler telling you to
check them. A client-originated payload is the usual case:
Events.onClient("kit:asked", (...args: unknown[]) => { const player = args[0]; if (!(player instanceof Player)) return; player.giveItem(0x1f66a, 5);});Editor setup
Section titled “Editor setup”Visual Studio Code needs nothing beyond the project: open the resource folder and the TypeScript
extension picks up tsconfig.json. If completions are missing, check that npm install has run and
that the file you are editing is covered by include.
Related
Section titled “Related”- Write your first resource: the same resource in plain JavaScript.
- Structure a larger resource: where the files go once there are more of them.