Overview
The types module provides all fundamental type definitions used throughout chessops, including chess piece types, board coordinates, move representations, and game rule variants.
Constants
FILE_NAMES
const FILE_NAMES: readonly ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Array of all chess file names from a-h.
RANK_NAMES
const RANK_NAMES: readonly ['1', '2', '3', '4', '5', '6', '7', '8']
Array of all chess rank names from 1-8.
ROLE_CHARS
const ROLE_CHARS: readonly ['q', 'n', 'r', 'b', 'p', 'k']
Array of single-character piece role identifiers.
COLORS
const COLORS: readonly ['white', 'black']
Array of valid chess piece colors.
ROLES
const ROLES: readonly ['pawn', 'knight', 'bishop', 'rook', 'queen', 'king']
Array of all chess piece roles.
CASTLING_SIDES
const CASTLING_SIDES: readonly ['a', 'h']
Array of castling sides, represented by the file of the rook involved.
RULES
const RULES: readonly [
'chess',
'antichess',
'kingofthehill',
'3check',
'atomic',
'horde',
'racingkings',
'crazyhouse'
]
Array of supported chess variant rule sets.
Type Aliases
FileName
type FileName = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h'
A chess file name (column).
RankName
type RankName = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8'
A chess rank name (row).
Square
A square index from 0 to 63, where 0 is a1 and 63 is h8. Calculated as file + 8 * rank.
Example:
const a1: Square = 0;
const e4: Square = 4 + 8 * 3; // 28
const h8: Square = 63;
SquareName
type SquareName = `${FileName}${RankName}`
A square in algebraic notation, combining a file and rank name.
Example:
const square: SquareName = 'e4';
const corner: SquareName = 'a1';
RoleChar
type RoleChar = 'q' | 'n' | 'r' | 'b' | 'p' | 'k'
Single-character representation of a piece role.
Color
type Color = 'white' | 'black'
A chess piece color.
Role
type Role = 'pawn' | 'knight' | 'bishop' | 'rook' | 'queen' | 'king'
A chess piece role.
CastlingSide
type CastlingSide = 'a' | 'h'
The side on which castling occurs, represented by the file of the rook involved.
Rules
type Rules = 'chess' | 'antichess' | 'kingofthehill' | '3check' | 'atomic' | 'horde' | 'racingkings' | 'crazyhouse'
Supported chess variant rule sets.
BySquare<T>
An array indexable by square indices (0-63).
Example:
const pieceOnSquare: BySquare<Piece | undefined> = new Array(64);
pieceOnSquare[0] = { role: 'rook', color: 'white' }; // a1
ByColor<T>
type ByColor<T> = {
white: T;
black: T;
}
An object indexable by color (white and black).
Example:
const materialCount: ByColor<number> = {
white: 39,
black: 39
};
ByRole<T>
type ByRole<T> = {
pawn: T;
knight: T;
bishop: T;
rook: T;
queen: T;
king: T;
}
An object indexable by piece role.
Example:
const pieceValues: ByRole<number> = {
pawn: 1,
knight: 3,
bishop: 3,
rook: 5,
queen: 9,
king: 0
};
ByCastlingSide<T>
type ByCastlingSide<T> = {
a: T;
h: T;
}
An object indexable by castling side (a and h).
Example:
const rookSquares: ByCastlingSide<Square> = {
a: 0, // a1
h: 7 // h1
};
Move
type Move = NormalMove | DropMove
A chess move, either a normal move or a drop move (for variants like Crazyhouse).
Interfaces
Piece
interface Piece {
role: Role;
color: Color;
promoted?: boolean;
}
Represents a chess piece.
The piece role (pawn, knight, bishop, rook, queen, or king).
The piece color (white or black).
Whether this piece was promoted from a pawn. Used in variants like Crazyhouse where promoted pieces behave differently.
Example:
const whitePawn: Piece = { role: 'pawn', color: 'white' };
const blackQueen: Piece = { role: 'queen', color: 'black', promoted: true };
NormalMove
interface NormalMove {
from: Square;
to: Square;
promotion?: Role;
}
A standard chess move from one square to another.
The destination square index.
The piece role to promote to, if this is a pawn promotion.
Example:
const e2e4: NormalMove = { from: 12, to: 28 };
const promotion: NormalMove = { from: 48, to: 56, promotion: 'queen' };
DropMove
interface DropMove {
role: Role;
to: Square;
}
A drop move used in variants like Crazyhouse, where a captured piece is placed back on the board.
The piece role being dropped.
The destination square index.
Example:
const dropKnight: DropMove = { role: 'knight', to: 28 }; // Drop knight on e4
Outcome
interface Outcome {
winner: Color | undefined;
}
Represents the outcome of a chess game.
winner
Color | undefined
required
The winning color, or undefined for a draw.
Example:
const whiteWins: Outcome = { winner: 'white' };
const draw: Outcome = { winner: undefined };
Type Guards
isDrop
const isDrop: (v: Move) => v is DropMove
Type guard to check if a move is a drop move.
Returns: true if the move is a DropMove, false otherwise.
Example:
const move: Move = { role: 'knight', to: 28 };
if (isDrop(move)) {
console.log(`Dropping ${move.role}`);
}
isNormal
const isNormal: (v: Move) => v is NormalMove
Type guard to check if a move is a normal move.
Returns: true if the move is a NormalMove, false otherwise.
Example:
const move: Move = { from: 12, to: 28 };
if (isNormal(move)) {
console.log(`Moving from ${move.from} to ${move.to}`);
}