Typescript declaration file for chess.js

2019-05-18 16:06发布

问题:

I'm trying to pick up typescript and working on creating a declaration file for the chess.js lib https://github.com/jhlywa/chess.js. It seems I do not have the grasp of how to make one. When I try to import the lib using the import statement

import { Chess } from chess.js

It complains that Module chess.js has no exported members.

This is what I have so far I'm the index.d.ts file.

declare namespace ChessJSTypes {
    type ChessType = 'p' | 'n' | 'b' | 'r' | 'q' | 'k';

    type ChessColor = 'b' | 'w';
}

interface MoveOptions {
    sloppy: boolean;
}

interface HistoryOptions {
    verbose: boolean;
}

interface MovesOptions {
    legal?: boolean;
    square?: string;
    verbose?: boolean;
}

interface PgnOptions {
    sloppy?: boolean;
    max_width?: number;
    newline_char?: string;
}

interface IFlags {
    NORMAL: string;
    CAPTURE: string;
    BIG_PAWN: string;
    EP_CAPTURE: string;
    PROMOTION: string;
    KSIDE_CASTLE: string;
    QSIDE_CASTLE: string;
}

interface Move {
    to: string;
    from: string;
    san?: string;
    flags?: string;
    piece?: string;
    color?: string;
    captured?: string;
    promotion?: string;
}

interface ValidationObject {
    error: string;
    valid: boolean;
    error_number: number;
}

interface ChessPiece {
    type: ChessJSTypes.ChessType;
    color: ChessJSTypes.ChessColor;
}

declare class Chess {
    readonly PAWN: string;
    readonly KNIGHT: string;
    readonly BISHOP: string;
    readonly ROOK: string;
    readonly QUEEN: string;
    readonly KING: string;
    readonly BLACK: string;
    readonly WHITE: string;
    readonly FLAGS: IFlags;

    constructor(fen: string);
    constructor();

    board(): ChessPiece[][];

    load(fen: string): boolean;

    reset(): void;

    moves(options?: MovesOptions): string[] | Move[];

    in_check(): boolean;

    in_checkmate(): boolean;

    in_stalemate(): boolean;

    in_draw(): boolean;

    insufficient_material(): boolean;

    in_threefold_repetition(): boolean;

    game_over(): boolean;

    validate_fen(fen: string): ValidationObject;

    fen(): string;

    pgn(option?: PgnOptions): string;

    load_pgn(pgn: string, options?: PgnOptions): boolean;

    header(args?: any): void;
    header(): any;

    ascii(): string;

    turn(): string;

    move(move: string | Move, options?: MoveOptions): Move;

    undo(): Move;

    clear(): void;

    put(piece: ChessPiece, square: string): boolean;

    get(square: string): ChessPiece;

    remove(square: string): ChessPiece;

    perft(depth: number): number;

    square_color(square: string): string;

    history(options: HistoryOptions): Move[];
    history(): string[];
}

declare module 'chess.js' {
var Chess: Chess;

export = Chess;
}

回答1:

One thing you could do is to declare the module and move everything inside, without trying to export anything within the declaration:

import * as Chess from 'chess.js';

declare module 'chess.js' {
    class Chess {
        // ...
    }
}