【问题标题】:Typescript declaration file for chessboardjs (implicit import)chessboardjs 的打字稿声明文件(隐式导入)
【发布时间】:2017-09-14 21:03:15
【问题描述】:

希望为 DefinitiveTyped 创建一些声明文件(所以我想确保它们是最高质量的)。

我要使用的下一个库是https://github.com/oakmac/chessboardjs/。如果我像这样导入它,我实际上可以正常工作

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

现在我可以通过调用来使用库

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

问题是我希望 import 语句是隐式的(ES6 风格),但不知道该怎么做

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

如果可能的话,我想要一些有关如何执行此操作的指导。由于我对打字稿和声明文件还很陌生,也许这个库不是为隐式导入而构建的

这是我目前在 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;
}

谢谢!!!

【问题讨论】:

  • 嘿,只是想知道——你在这个库的类型上取得了进一步的进展吗?
  • 我想我做到了。不过,我还没有把它推到确定类型。如果你也在做类似的事情,我可以同时与你分享
  • 我正在做一些可以极大地受益于国际象棋和棋盘的打字的东西。您可以通过 gmail dot com 的 rikkigibson 与我联系。 (抱歉,没有看到任何其他传达联系信息的方式。)
  • Np。我会在几分钟内将它们发送到您的电子邮件中

标签: javascript angular typescript npm typescript-typings


【解决方案1】:

它不是这样工作的。定义文件描述这个库是如何工作的。

这个

import * as ChessBoard from "chessboardjs"

还有这个

import ChessBoard from "chessboardjs"

还有这个

import { ChessBoard } from "chessboardjs"

每个在运行时都意味着三个非常不同的东西。几乎可以肯定,其中只有一个有效。如果你有一个有效的导入,你根本不应该接触定义文件​​。它只会在运行时中断。

【讨论】:

  • 谢谢瑞恩。作为定义文件的作者,我假设我对如何构建它有一定的灵活性,只要它在最后描述了 Lib。我为我的菜鸟道歉,我仍处于学习模式。假设灵活性不扩展到导入/导出样式是否安全?
猜你喜欢
  • 1970-01-01
  • 2017-09-12
  • 2018-03-11
  • 1970-01-01
  • 2017-02-16
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
相关资源
最近更新 更多