Runtime Declarations
Runtime type declarations in Spinorium allow you to extend and customize the engine's interfaces and types. This is done through declaration merging, a TypeScript feature that lets you add new properties to existing types.
Understanding runtime.d.ts
The runtime.d.ts file is a TypeScript declaration file, usually located in the source folder of the project. Using this file provides several benefits:
- Eliminates excessive type casting throughout the codebase.
- Enables overriding of existing interfaces from the engine.
- Maintains type safety for custom game-specific controllers and models.
- Defines game-specific data structures.
Usage Example
import { GameController } from './controller/GameController';
import { GamePlayerModel } from './controller/GamePlayerModel';
import { GameRoundModel } from './controller/GameRoundModel';
import { GameRoundState } from './controller/GameRoundState';
declare module '@spinorium/engine' {
interface SystemContext {
readonly model:GamePlayerModel;
readonly controller:GameController;
readonly state:Readonly<GameRoundState>;
readonly round:GameRoundModel;
}
interface ISymbolState {
upgradeLevel?:number;
}
}
In the example above:
-
The
SystemContextinterface is extended to include core game-specific properties. These are accessible through theSystemglobal constant (e.g.,System.round.roundBet). -
The
ISymbolStateinterface is enhanced with a new propertyupgradeLevel. This enables type-safe usage of the property across the game code without the need for manual casting.
const symbol = slotMachine.factory.compose({
// Property now available and type-safe
upgradeLevel: 1,
index: SymbolType.HP_1,
sourceType: SymbolSourceType.REAL,
stateType: 'idle'
});
// Property now available and type-safe
// You'll see "1" in the console
console.log(symbol.state.upgradeLevel);