chess.js的打字稿声明文件

时间:2017-04-16 13:53:01

标签: javascript angular typescript npm typescript-typings

我正在尝试选择typescript并为chess.js lib https://github.com/jhlywa/chess.js创建一个声明文件。似乎我没有掌握如何制作一个。当我尝试使用import语句

导入lib时
import { Chess } from chess.js

它抱怨Module chess.js没有导出成员。

到目前为止,这是我的index.d.ts文件。

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 个答案:

答案 0 :(得分:0)

你可以做的一件事就是声明模块并将所有内容移到里面,而不是试图在声明中导出任何东西:

View