Skip to lesson
Flawless Poker
Galaxy Bot AcademyProgramming Languages and Coding Techniques
INTERMEDIATE · 16 MIN READ · PROGRAMMING LANGUAGES AND CODING TECHNIQUES

TypeScript: Types, Validation, and Strategy

Learning objectiveModel the needed observation fields and run type-safe strategy code.

Understand the idea

A TypeScript interface records the fields your strategy reads. It does not validate untrusted JSON at runtime, so the parser must still check object shapes before using them. Avoid copying the full server type unless your strategy needs every field.

For a compiled bot, install TypeScript locally, compile to JavaScript, and set FLAWLESS_BOT_COMMAND to node dist/bot.js. A TypeScript runner can also be used if you have one installed. The universal adapter still communicates with the resulting process through stdin/stdout.

At the poker table

A legal raise target is constrained by minTo and maxTo at runtime even if TypeScript says it is a number.

Code and decision sketch

TypeScript
type Legal = { check?: boolean; call?: boolean; callAmount?: number; fold?: boolean };
type Observation = { legal?: Legal; you?: { stack?: number } };
type Action = { kind: 'check' | 'call' | 'fold' };

function choose(obs: Observation): Action {
  const legal = obs.legal ?? {};
  const stack = obs.you?.stack ?? 0;
  if (legal.check === true) return { kind: 'check' };
  if (legal.call === true && typeof legal.callAmount === 'number' &&
      legal.callAmount <= Math.min(20, stack)) return { kind: 'call' };
  if (legal.fold === true) return { kind: 'fold' };
  throw new Error('Unsupported legal state');
}
// Validate JSON.parse(input) before passing it to choose.

Common mistake

A type assertion such as JSON.parse(line) as Observation does not prove that the network data actually has that shape.

Try it yourself

Add a runtime guard that rejects an observation whose legal field is a string rather than an object.