Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
323 changes: 164 additions & 159 deletions @types/chess/chess.d.ts
Original file line number Diff line number Diff line change
@@ -1,190 +1,195 @@
// Typescript Declaration File for Node Chess module
// https://www.npmjs.com/package/chess

declare namespace Chess {
export function create(opts?: { PGN: boolean }): AlgebraicGameClient
export function createSimple(): SimpleGameClient
export function fromFEN(fen: string, opts?: { PGN: boolean }): AlgebraicGameClient
export function createUCI(): UCIGameClient

interface GameStatus {
/** Whether either of the side is under check */
isCheck: boolean
/** Whether either of the side is checkmated */
isCheckMate: boolean
/** Whether game has ended by threefold repetition */
isRepetition: boolean
/** Whether game has ended by stalemate */
isStalemate: boolean
}
export function create(opts?: { PGN: boolean }): AlgebraicGameClient
export function createSimple(): SimpleGameClient
export function fromFEN(fen: string, opts?: { PGN: boolean }): AlgebraicGameClient
export function createUCI(): UCIGameClient

export interface GameStatus {
/** Whether either of the side is under check */
isCheck: boolean
/** Whether either of the side is checkmated */
isCheckMate: boolean
/** Whether game has ended by threefold repetition */
isRepetition: boolean
/** Whether game has ended by stalemate */
isStalemate: boolean
}

interface SimpleGameStatus extends GameStatus {
/** Current board configuration */
board: ChessBoard
}
export interface SimpleGameStatus extends GameStatus {
/** Current board configuration */
board: ChessBoard
}

interface AlgebraicGameStatus extends SimpleGameStatus {
/** Hash of next possible moves with key as notation and value as src-dest mapping */
notatedMoves: Record<string, NotatedMove>
}
export interface AlgebraicGameStatus extends SimpleGameStatus {
/** Hash of next possible moves with key as notation and value as src-dest mapping */
notatedMoves: Record<string, NotatedMove>
}

interface UCIGameStatus extends SimpleGameStatus {
/** Hash of next possible moves with key as UCI string and value as src-dest mapping */
uciMoves: Record<string, NotatedMove>
}
export interface UCIGameStatus extends SimpleGameStatus {
/** Hash of next possible moves with key as UCI string and value as src-dest mapping */
uciMoves: Record<string, NotatedMove>
}

interface GameClient extends GameStatus {
/** The Game object, which includes the board and move history. */
game: Game
/** An array of pieces (src) which can move to the different squares. */
validMoves: ValidMove[]
/** Game Validation Object */
validation: GameValidation
/** Add event listeners */
on(event: ChessEvent, cbk: () => void): void
/**
* Make a move on the board
* @param notation Notation of move in PGN format
*/
move(notation: string): PlayedMove
getStatus(): AlgebraicGameStatus | SimpleGameStatus
/** Returns the list of captured pieces in order */
getCaptureHistory(): Piece[]
}
export interface GameClient extends GameStatus {
/** The Game object, which includes the board and move history. */
game: Game
/** An array of pieces (src) which can move to the different squares. */
validMoves: ValidMove[]
/** Game Validation Object */
validation: GameValidation
/** Add event listeners */
on(event: ChessEvent, cbk: () => void): void
/**
* Make a move on the board
* @param notation Notation of move in PGN format
*/
move(notation: string): PlayedMove
getStatus(): AlgebraicGameStatus | SimpleGameStatus
/** Returns the list of captured pieces in order */
getCaptureHistory(): Piece[]
}

interface SimpleGameClient extends GameClient {
getStatus(): SimpleGameStatus
}
export interface SimpleGameClient extends GameClient {
getStatus(): SimpleGameStatus
}

interface AlgebraicGameClient extends GameClient {
/** Hash of next possible moves with key as notation and value as src-dest mapping */
notatedMoves: Record<string, NotatedMove>
/** Whether notation is safe for PGN or not */
PGN: boolean
getStatus(): AlgebraicGameStatus
getFen(): string
}
export interface AlgebraicGameClient extends GameClient {
/** Hash of next possible moves with key as notation and value as src-dest mapping */
notatedMoves: Record<string, NotatedMove>
/** Whether notation is safe for PGN or not */
PGN: boolean
getStatus(): AlgebraicGameStatus
getFen(): string
}

interface UCIGameClient extends GameStatus {
game: Game
validMoves: ValidMove[]
validation: GameValidation
on(event: ChessEvent, cbk: () => void): void
/** Make a move on the board using UCI notation */
move(uci: string): PlayedMove
getStatus(): UCIGameStatus
/** Returns the list of captured pieces in order */
getCaptureHistory(): Piece[]
}
export interface UCIGameClient extends GameStatus {
game: Game
validMoves: ValidMove[]
validation: GameValidation
on(event: ChessEvent, cbk: () => void): void
/** Make a move on the board using UCI notation */
move(uci: string): PlayedMove
getStatus(): UCIGameStatus
/** Returns the list of captured pieces in order */
getCaptureHistory(): Piece[]
}

type File = string
type Rank = number
type ChessEvent = 'check' | 'checkmate'

interface PlayedMove {
move: {
algebraic: string
capturedPiece: Piece
castle: boolean
enPassant: boolean
postSquare: Square
prevSquare: Square
}
undo(): void
}
export type File = string
export type Rank = number
export type ChessEvent = 'check' | 'checkmate'

interface Game {
board: ChessBoard
captureHistory: Piece[]
moveHistory: Move[]
export interface PlayedMove {
move: {
algebraic: string
capturedPiece: Piece
castle: boolean
enPassant: boolean
postSquare: Square
prevSquare: Square
}
undo(): void
}

interface GameValidation {
game: Game
}
export interface Game {
board: ChessBoard
captureHistory: Piece[]
moveHistory: Move[]
}

interface ChessBoard {
squares: Square[]
lastMovedPiece: Piece
}
export interface GameValidation {
game: Game
}

interface Move {
algebraic: string
capturedPiece: Piece
hashCode: string
piece: Piece
promotion: boolean
postFile: File
postRank: Rank
prevFile: File
prevRank: Rank
}
export interface ChessBoard {
squares: Square[]
lastMovedPiece: Piece
}

interface NotatedMove {
dest: Square
src: Square
}
export interface Move {
algebraic: string
capturedPiece: Piece
hashCode: string
piece: Piece
promotion: boolean
postFile: File
postRank: Rank
prevFile: File
prevRank: Rank
}

interface ValidMove {
squares: Square[]
src: Square
}
export interface NotatedMove {
dest: Square
src: Square
}

interface Square {
file: File
piece: Piece
rank: Rank
}
export interface ValidMove {
squares: Square[]
src: Square
}

interface Side {
name: 'white' | 'black'
}
export interface Square {
file: File
piece: Piece
rank: Rank
}

interface IPiece {
type: string
notation: string
moveCount: number
side: Side
}
export interface Side {
name: 'white' | 'black'
}

class AbstractPiece implements IPiece {
type: string
notation: string
moveCount: number
side: Side
}
export interface IPiece {
type: string
notation: string
moveCount: number
side: Side
}

type Piece = Pawn | Knight | Bishop | Rook | Queen | King
export class AbstractPiece implements IPiece {
type: string
notation: string
moveCount: number
side: Side
}

class Pawn extends AbstractPiece {
notation: ''
type: 'pawn'
}
export type Piece = Pawn | Knight | Bishop | Rook | Queen | King

class Knight extends AbstractPiece {
notation: 'N'
type: 'knight'
}
export class Pawn extends AbstractPiece {
notation: ''
type: 'pawn'
}

class Bishop extends AbstractPiece {
notation: 'B'
type: 'bishop'
}
export class Knight extends AbstractPiece {
notation: 'N'
type: 'knight'
}

class Rook extends AbstractPiece {
notation: 'R'
type: 'rook'
}
export class Bishop extends AbstractPiece {
notation: 'B'
type: 'bishop'
}

class Queen extends AbstractPiece {
notation: 'Q'
type: 'queen'
}
export class Rook extends AbstractPiece {
notation: 'R'
type: 'rook'
}

class King extends AbstractPiece {
notation: 'K'
type: 'king'
}
export class Queen extends AbstractPiece {
notation: 'Q'
type: 'queen'
}

export = Chess
export class King extends AbstractPiece {
notation: 'K'
type: 'king'
}

export declare const Chess: {
create: typeof create;
createSimple: typeof createSimple;
fromFEN: typeof fromFEN;
createUCI: typeof createUCI;
};

export default Chess;
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
}
],
"exports": {
"import": "./src/main.js",
"require": "./cjs/main.cjs"
".": {
"import": "./src/main.js",
"require": "./cjs/main.cjs",
"types": "./@types/chess/chess.d.ts"
}
},
"type": "module",
"engine": "node >= 16.0",
Expand Down