我正在尝试选择typescript并为chess.js libhttps://github.com/jhlywa/chess.js创建一个声明文件。我好像不知道怎么做。当我尝试使用import语句导入lib时

import { Chess } from chess.js

它抱怨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;
}

最佳答案

您可以做的一件事是声明模块并将所有内容移入其中,而不尝试导出声明中的任何内容:

import * as Chess from 'chess.js';

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

关于javascript - 国际象棋的 typescript 声明文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43437964/

10-09 18:21
查看更多