简体   繁体   English

chessboardjs的Typescript声明文件(隐式导入)

[英]Typescript declaration file for chessboardjs (implicit import)

Hoping to create a few declaration files for DefinitelyTyped (So I want to make sure they are top quality). 希望为DefinitelyTyped创建一些声明文件(所以我想确保它们是最高质量的)。

The next lib I am taking on is https://github.com/oakmac/chessboardjs/ . 我正在接受的下一个lib是https://github.com/oakmac/chessboardjs/ I actually have it working if I import it like so 如果我像这样导入它,我实际上有它工作

// WHAT WORKS
import * as ChessBoard from "chessboardjs";

and now i can use the lib by calling 现在我可以通过调用来使用lib

const board = ChessBoard('board1', 'start');

The problem is i want the import statement to be implicit (ES6 style) and not sure how to go by doing that 问题是我希望import语句是隐式的(ES6样式),并且不确定如何通过这样做

// WHAT I WOULD LIKE
import { ChessBoard } from "chessboardjs";

I would like some guidance on how to go by doing this if possible. 如果可能的话,我想就如何做到这一点提供一些指导。 As I'm still new to typescript and declaration files, maybe the lib just isn't built for implicit imports 由于我还是打字稿和声明文件的新手,也许lib不是为隐式导入而构建的

This is what i have so far in the index.d.ts file 这是我目前在index.d.ts文件中所拥有的

declare namespace ChessBoardJS {
    interface BoardConfig {
        onDrop?: Function;
        draggable?: boolean;
        onChange?: Function;
        onMoveEnd?: Function;
        onSnapEnd?: Function;
        sparePieces?: boolean;
        onDragMove?: Function;
        showNotation?: boolean;
        onDragStart?: Function;
        onSnapbackEnd?: Function;
        onMouseoutSquare?: Function;
        onMouseoverSquare?: Function;
        pieceTheme?: string | Function;
        orientation?: ChessBoardJS.Types.OrientationType;
        showErrors?: boolean | string | Function;
        moveSpeed?: number | ChessBoardJS.Types.SpeedType;
        snapSpeed?: number | ChessBoardJS.Types.SpeedType;
        trashSpeed?: number | ChessBoardJS.Types.SpeedType;
        dropOffBoard?: ChessBoardJS.Types.DropOffBoardType;
        appearSpeed?: number | ChessBoardJS.Types.SpeedType;
        snapbackSpeed?: number | ChessBoardJS.Types.SpeedType;
        position?: ChessBoardJS.Types.PositionType | string | object;
    }
}

declare namespace ChessBoardJS.Types {
    type PositionType = 'start';
    type PositionFenType = 'fen';
    type SpeedType = 'slow' | 'fast';
    type OrientationFlipType = 'flip';
    type OrientationType = 'white' | 'black';
    type DropOffBoardType = 'snapback' | 'trash';
}

interface ChessBoardInstance {
    clear(useAnimation?: boolean): void;
    destroy(): void;
    fen(): string;
    flip(): void;
    move(...args: string[]): object; // *FIND RETURN*
    position(newPosition: object | string | ChessBoardJS.Types.PositionType, useAnimation?: boolean): void
    position(fen?: ChessBoardJS.Types.PositionFenType): string | object;
    orientation(side?: ChessBoardJS.Types.OrientationType | ChessBoardJS.Types.OrientationFlipType): string;
    resize(): void;
    start(useAnimation?: boolean): void;
}

interface ChessBoardFactory {
    (containerElOrId: any, config: ChessBoardJS.BoardConfig): ChessBoardInstance
    fenToObj(fen: string): any;
    objToFen(obj: any): any;
}

declare var ChessBoard: ChessBoardFactory;
declare module "chessboardjs" {
    export = ChessBoard;
}

Thank you!!! 谢谢!!!

It doesn't work like this. 它不像这样工作。 The definition file describes how the library works. 定义文件描述了库的工作方式。

This 这个

import * as ChessBoard from "chessboardjs"

and this 和这个

import ChessBoard from "chessboardjs"

and this 和这个

import { ChessBoard } from "chessboardjs"

each mean three very different things at runtime. 每个在运行时意味着三个截然不同的东西 Almost certainly only one of them works. 几乎可以肯定,其中只有一个有效。 If you have a working import, you shouldn't be touching the definition file at all. 如果您有工作导入,则根本不应触及定义文件。 It's just going to break at runtime. 它只会在运行时中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM