簡體   English   中英

無法執行無類型打字稿模塊

[英]Failed execution of untyped typescript module

我正在嘗試將Typescript與Webpack集成到項目構建中。 我有一個不帶ts定義的庫( Chess.js ),所以我使用了一個外部定義庫( @ types / chess.js )。

我設法使代碼得以編譯而沒有任何錯誤,但是在瀏覽器中執行該代碼時,出現以下錯誤:

TypeError: i.Chess is not a constructor

這是無效的打字稿代碼:

import {Chess} from 'chess.js';

const board = new Chess();

Chess.js模塊沒有關聯的打字稿類型。 這是此模塊的導出方式。

var Chess = function(fen) {
    // ...
}

/* export Chess object if using node or any other CommonJS compatible
 * environment */
if (typeof exports !== 'undefined') exports.Chess = Chess;
/* export Chess object for any RequireJS compatible environment */
if (typeof define !== 'undefined') define( function () { return Chess;  });

這是我使用的聲明chess.js模塊的ts類型。

/**
 * The chess.js function that is used to build chess game instances.
 * It can be used with or without `new` to build your instance. Both variants
 * work the same.
 */
export const Chess: {
    (fen?: string): ChessInstance;

    new (fen?: string): ChessInstance;
};

這是我的tsconfig.json:

這是我的tsconfig文件:

{
  "compilerOptions": {
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es5",
    "allowJs": true
  }
}

我使用的webpack配置與webpack基本TS設置指南中定義的相同。

同時聲明模塊

declare module 'chess.js' {
  type ChessInstance = any
  export const Chess: {
    (fen?: string): ChessInstance;

    new (fen?: string): ChessInstance;
  };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM